I fully agree with your assessment about using InvokeRequired in the
control itself to make the control threadsafe. I personally fail to
understand why this is not so to beginwith in Microsoft controls which
are essentially nothing more than wrappers for controls in
Comctl32.dll.
I first saw this in Chris Sells's book on WinForms and have used it
since then. This makes life easy for the clients as they dont have to
worry about providing delegates each time they call controls from the
worker thread. In addition, keeping the thread logic within a control
makes it easy to remove/add worker thread.
--------
Ajay Kalra
(e-mail address removed)
Thanks Ajay,
I truely did not think there was anything that could help me. This tool
does
raise a few questions for me though...
I have tried to separate my UI layer such that I have UI classes
(typically
UserControls) and I have Controller classes that provide data to the UI
classes from web services. All web service calls that a controller
makes is
asynchronous and I give it a callback to use to notify me of
completion.
This means on completion of the web service call I'm giving data to the
UI
class inside my callback and hence from a worker thread.
Because this is a common occurance for my application I tried to make
my UI
classes thread aware. I did this by adding a call to InvokeRequired()
and
(if necessary) Invoke() at the top of all myl public methods, this
means
that I can call into me UI class from any thread and it would look
after its
own marshelling. However, there was a problem here. All public
UserControl
methods does not have the same sort of build in protection, so...
One big question I have is; why don't the Windows forms controls do
this for
us? Does it really hurt performance? Isn't it as simple as say:
public bool Focus()
{
if(InvokeRequired)
Invoke(new MethodInvoker(Focus));
else
{
// usual code here
}
}
Any insite would be really great.
Thanks
Graham
Try the following. I have not used it but I only read about it
earlier
today. The idea sounds good:
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=3cba6
4b4-3765-49ba-8a05-11d247195e65
--
Ajay Kalra [MVP - VC++]
(e-mail address removed)
Can anyone suggest a good (cheap) tool that will detect when a
worker
thread
calls directly in to a control on the UI thread?
Thanks for your help
Graham