Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing Control.Focused property from Thread

Accessing Control.Focused property from Thread

Scheduled Pinned Locked Moved C#
help
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sukhjinder_K
    wrote on last edited by
    #1

    Hi I need to get the Focused Property of My ListBox list from within a thread. The thread makes calls to following function bool listFocused() { if (this.list.InvokeRequired) { ListFocusCallBack d = new ListFocusCallBack(listFocused); Object obj = this.list.Invoke(d, new object[] {}); // ProblemLine // Should Return something here } else { return this.list.Focused; } } If I compile the above then I get a "not all code paths return a value" error, because I'm not returning anything in "ProblemLine" I tried replacing "ProblemLine" with if( obj.Equals(Boolean.TrueString )) return true; return false; but I'm not sure if it is the right approach. Please Advice Thanks...

    M 1 Reply Last reply
    0
    • S Sukhjinder_K

      Hi I need to get the Focused Property of My ListBox list from within a thread. The thread makes calls to following function bool listFocused() { if (this.list.InvokeRequired) { ListFocusCallBack d = new ListFocusCallBack(listFocused); Object obj = this.list.Invoke(d, new object[] {}); // ProblemLine // Should Return something here } else { return this.list.Focused; } } If I compile the above then I get a "not all code paths return a value" error, because I'm not returning anything in "ProblemLine" I tried replacing "ProblemLine" with if( obj.Equals(Boolean.TrueString )) return true; return false; but I'm not sure if it is the right approach. Please Advice Thanks...

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, You don't need the else statement in your method. If invoke is called the code after that will not been worked threw. Set a breakpoint at the first line of the method and Debug it to see it better. Your method could look like that: bool listFocused() { if(this.list.InvokeRequired) { ListFocusCallBack d = new ListFocusCallBack(listFocused); this.list.Invoke(d, new object[] {}); } return this.list.Focused; } -- modified at 8:07 Wednesday 21st November, 2007 Wrong Information Sorry

      All the best, Martin

      S 1 Reply Last reply
      0
      • M Martin 0

        Hello, You don't need the else statement in your method. If invoke is called the code after that will not been worked threw. Set a breakpoint at the first line of the method and Debug it to see it better. Your method could look like that: bool listFocused() { if(this.list.InvokeRequired) { ListFocusCallBack d = new ListFocusCallBack(listFocused); this.list.Invoke(d, new object[] {}); } return this.list.Focused; } -- modified at 8:07 Wednesday 21st November, 2007 Wrong Information Sorry

        All the best, Martin

        S Offline
        S Offline
        Stevo Z
        wrote on last edited by
        #3

        To my knowledge, it's not thread safe this way, let me explain :

        bool listFocused()
        {
        // lets say this method is called from thread [T1], different from main application thread [T0] (thread that that listbox was created on)

        if(this.list.InvokeRequired) // if this condition is true, we are on \[T1\]
        {
            ListFocusCallBack d = new ListFocusCallBack(listFocused); // still \[T1\]
            this.list.Invoke(d, new object\[\] {}); // 'd' executes on \[T0\] !! \[T1\] is blocked until Invoke finishes
        
           // now we're back at \[T1\] !!
        }
        
        return this.list.Focused; // !!! again \[T1\] - if u invoke a delegate using control invoke, it doesn't mean
                                  // that the rest of this method will be executed on that invoked thread. it
                                  // remains on thread \[T1\], where it's not safe to get the Focused value.
        

        }

        what you need to do is to return value that Control.Invoke method returns

        bool listFocused()
        {
        // [T1]

        if(this.list.InvokeRequired) // if on other than \[T0\]
        {
             ListFocusCallBack d = new ListFocusCallBack(listFocused);
             return (bool)this.list.Invoke(d, new object\[\] {}); // wait for Invoked method to finish and return result (again on \[T1\])       
        }
        
        else // \[T0\] now we are sure this is thread safe 
        {
            return this.list.Focused; 
        } 
        

        }

        zilo

        M S 2 Replies Last reply
        0
        • S Stevo Z

          To my knowledge, it's not thread safe this way, let me explain :

          bool listFocused()
          {
          // lets say this method is called from thread [T1], different from main application thread [T0] (thread that that listbox was created on)

          if(this.list.InvokeRequired) // if this condition is true, we are on \[T1\]
          {
              ListFocusCallBack d = new ListFocusCallBack(listFocused); // still \[T1\]
              this.list.Invoke(d, new object\[\] {}); // 'd' executes on \[T0\] !! \[T1\] is blocked until Invoke finishes
          
             // now we're back at \[T1\] !!
          }
          
          return this.list.Focused; // !!! again \[T1\] - if u invoke a delegate using control invoke, it doesn't mean
                                    // that the rest of this method will be executed on that invoked thread. it
                                    // remains on thread \[T1\], where it's not safe to get the Focused value.
          

          }

          what you need to do is to return value that Control.Invoke method returns

          bool listFocused()
          {
          // [T1]

          if(this.list.InvokeRequired) // if on other than \[T0\]
          {
               ListFocusCallBack d = new ListFocusCallBack(listFocused);
               return (bool)this.list.Invoke(d, new object\[\] {}); // wait for Invoked method to finish and return result (again on \[T1\])       
          }
          
          else // \[T0\] now we are sure this is thread safe 
          {
              return this.list.Focused; 
          } 
          

          }

          zilo

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello Zilo, I do not agree with you.

          Zilo(svk) wrote:

          return this.list.Focused; // !!! again [T1] - if u invoke a delegate using control invoke, it doesn't mean // that the rest of this method will be executed on that invoked thread. it // remains on thread [T1], where it's not safe to get the Focused value.

          This code will never be executed by [T1] AFAIK. After the Invoke call, the method will no longer go threw the rest of the method code. I often debugged such a situation, and hope what I just told is not complete rubbish! -- modified at 8:09 Wednesday 21st November, 2007 I wrote: hope what I just told is not complete rubbish It was rubbish!

          All the best, Martin

          S 1 Reply Last reply
          0
          • M Martin 0

            Hello Zilo, I do not agree with you.

            Zilo(svk) wrote:

            return this.list.Focused; // !!! again [T1] - if u invoke a delegate using control invoke, it doesn't mean // that the rest of this method will be executed on that invoked thread. it // remains on thread [T1], where it's not safe to get the Focused value.

            This code will never be executed by [T1] AFAIK. After the Invoke call, the method will no longer go threw the rest of the method code. I often debugged such a situation, and hope what I just told is not complete rubbish! -- modified at 8:09 Wednesday 21st November, 2007 I wrote: hope what I just told is not complete rubbish It was rubbish!

            All the best, Martin

            S Offline
            S Offline
            Stevo Z
            wrote on last edited by
            #5

            Test it. It really is the way I wrote. I mean I don't see any reason why Control.Invoke(delegate Method); should for any reason make calling method to return whatever the delegate returns. It does just doesn't make any sense. Execution continues. It just blocks until delegate completed.

            zilo

            M 1 Reply Last reply
            0
            • S Stevo Z

              Test it. It really is the way I wrote. I mean I don't see any reason why Control.Invoke(delegate Method); should for any reason make calling method to return whatever the delegate returns. It does just doesn't make any sense. Execution continues. It just blocks until delegate completed.

              zilo

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello Zilo, Just testet it, and have to say THANK YOU SO MUCH for pointing that out. First of all, I was not consistend in my Projects. And second, I was completely wrong. Thats what I like at CP. By answering (or trying to do so at least), you also get support! Got my '5'. Thanks again!

              All the best, Martin

              S 1 Reply Last reply
              0
              • M Martin 0

                Hello Zilo, Just testet it, and have to say THANK YOU SO MUCH for pointing that out. First of all, I was not consistend in my Projects. And second, I was completely wrong. Thats what I like at CP. By answering (or trying to do so at least), you also get support! Got my '5'. Thanks again!

                All the best, Martin

                S Offline
                S Offline
                Stevo Z
                wrote on last edited by
                #7

                np, It's good to know that you know something :)) . I wasn't sure either about this one.

                zilo

                1 Reply Last reply
                0
                • S Stevo Z

                  To my knowledge, it's not thread safe this way, let me explain :

                  bool listFocused()
                  {
                  // lets say this method is called from thread [T1], different from main application thread [T0] (thread that that listbox was created on)

                  if(this.list.InvokeRequired) // if this condition is true, we are on \[T1\]
                  {
                      ListFocusCallBack d = new ListFocusCallBack(listFocused); // still \[T1\]
                      this.list.Invoke(d, new object\[\] {}); // 'd' executes on \[T0\] !! \[T1\] is blocked until Invoke finishes
                  
                     // now we're back at \[T1\] !!
                  }
                  
                  return this.list.Focused; // !!! again \[T1\] - if u invoke a delegate using control invoke, it doesn't mean
                                            // that the rest of this method will be executed on that invoked thread. it
                                            // remains on thread \[T1\], where it's not safe to get the Focused value.
                  

                  }

                  what you need to do is to return value that Control.Invoke method returns

                  bool listFocused()
                  {
                  // [T1]

                  if(this.list.InvokeRequired) // if on other than \[T0\]
                  {
                       ListFocusCallBack d = new ListFocusCallBack(listFocused);
                       return (bool)this.list.Invoke(d, new object\[\] {}); // wait for Invoked method to finish and return result (again on \[T1\])       
                  }
                  
                  else // \[T0\] now we are sure this is thread safe 
                  {
                      return this.list.Focused; 
                  } 
                  

                  }

                  zilo

                  S Offline
                  S Offline
                  Sukhjinder_K
                  wrote on last edited by
                  #8

                  I was also thinking on similar lines what I actually did was if (this.list.InvokeRequired) { ListFocusCallBack d = new ListFocusCallBack(listFocused); Object obj = this.list.Invoke(d, new object[] {}); if( obj.Equals(Boolean.TrueString )) return true; return false; } else { return this.list.Focused; } Your solution about converting Object to bool as (bool)obj never came to my mind. I never knew it was possible. Thanks... The Show Ain't Over Yet...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups