Toolbar not Threadsafe?

S

Sean Dudley

I have a toolbar control in my main form. I thread out a an instance of a
watcher class (just looking for new processes and raising an event when one
arises) as a background thread. my main form receives events from the
watcher thread.

Now, within the event handler on the main thread (which is firing just fine)
I attamept to add a new toolbar button to my toolbar based on the event
args. If I generate labels or standard form buttons everything is fine.
But if I try to add a button to a toolbar the program will seize up
(actually the toolbar control seems to lock up since it is the first item to
stop drawing itself). Sometimes I'll get a NullReference exception out of
the fiasco, other times I'll get a "Window handle already in use" error.
Neither of which make any sense given that everything is initialized and not
null, and all I am trying to do is add a button. If I do this identical
routine without threading out the watcher everything works fine.

The only difference between working versions and non-working is the
background thread approach.

Is this a bug?

Thanks!!
 
J

Justin Rogers

You have to check the InvokeRequired property of your Toolbar control to make
sure you don't
need to call Invoke to get the call on the primary UI thread. In your case you
think the the main
thread is handling the event you toss, but it probably isn't. you are probably
still on the watcher thread.
 
S

Sean Dudley

I am an idiot. I never used a delegate to invoke the toolbar's add
function. sorry for the post!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top