Accessing Control.Focused property from Thread
-
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" withif( obj.Equals(Boolean.TrueString )) return true; return false;
but I'm not sure if it is the right approach. Please Advice Thanks... -
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" withif( obj.Equals(Boolean.TrueString )) return true; return false;
but I'm not sure if it is the right approach. Please Advice Thanks...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 SorryAll the best, Martin
-
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 SorryAll the best, Martin
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
-
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
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
-
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
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
-
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
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
-
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
-
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
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...