This should be easy
-
Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
-
Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
You will need to Invoke everything coming from the other thread.
-
Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
When getting/setting properties or calling methods on a control from another thread, you need to use
Control.Invoke
. If you're not sure if you need to invoke the method (properties are just getter and setter methods), you can useControl.InvokeRequired
. More information and examples can be found in the .NET Framework SDK regarding these members.Microsoft MVP, Visual C# My Articles
-
When getting/setting properties or calling methods on a control from another thread, you need to use
Control.Invoke
. If you're not sure if you need to invoke the method (properties are just getter and setter methods), you can useControl.InvokeRequired
. More information and examples can be found in the .NET Framework SDK regarding these members.Microsoft MVP, Visual C# My Articles
So just because you declare a callback function doesn't mean it runs in the parent thread. Got it - thanks. Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
-
So just because you declare a callback function doesn't mean it runs in the parent thread. Got it - thanks. Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
It runs in the thread context in which it was invoked.
Microsoft MVP, Visual C# My Articles