views:

265

answers:

1

Hello everyone

I have got a C# user control, which has got it's own background worker thread. This worker thread is started in the constructor of the control and stopped when the control is disposed.

The thread periodically calls the BeginInvoke-Method with a delegate, but sometimes the exception "Invoke or BeginInvoke cannot be called on a control until the window handle has been created." occoures.

Now I ask you, how can I check whether calling BeginInvoke is possible from my worker thread to do no invoking as long as the control isn't completely created?

This problem only occoures when compiling a release. Not in debug mode.

with best regards

+2  A: 

The worker thread should be created in a handler to event Control.HandleCreated or by overriding Control.OnHandleCreated.

When done in constructor the handle of control may not be created (ready to be used).

Cedrik
Will try this. Thanks. Edit: Seems to work. I now start the thread when the HandleCreated Event occours and stop it when the HandleDestroyed event is fired.
Emiswelt