Freeze of Form on MultiThread!

Ö

Özden Irmak

Hello,

In my application, my main form creates some sub forms which are shown on
top of that main form and all of those forms do have a timer on them. When
the timer elapses, they do raise an event which is catched by the main
form...

My problem in here is that when all that events get raised and all the shown
sub forms have finished running their codes, my main form gets freezed...

For thread safety, I use this code in the main form event subscription
method but the result is still the same :

Control c = sender as Control;

if ( c != null )

{

if ( c.InvokeRequired )

{

c.Invoke(new EventHandler(this.MyEvent));

}

else

{

//Thread is safe, run the code

}

}

Can anybody help me what I'm missing in here?

Thanks,

Özden
 
A

Andrew Smith \(Infragistics\)

Are the subforms in the same thread as the form? If not, then the problem
could be that you are synchronizing based on the sender's thread instead of
on the main form's thread. Try changing it to use "this.InvokeRequired".
 
Ö

Özden Irmak

Hello Andrew,

Actually changing the timer types from System.Timers.Timer to
System.Windows.Forms.Timer did the trick...

Thanks for your help...

Regards,

Özden
 
S

Stoitcho Goutsev \(100\) [C# MVP]

If the sub-forms and the main form are created in the same thread then
changing the type fo the timer will do the trick because now you don't have
multithreading. The fact that you got the form freezing before changing the
timer type lets me think that the forms are created from different UI
threads in this case even with Windows.Forms.Timer you should use
Control.InvokeRequired and Control.Invoke otherwise you might get in
trouble. When you use Control.InvokeRequired and Control.Invoke use
reference to the form that is going to process the event rather than
reference to the sender of the event. If they are created in different UI
threads using the sender is wrong and most likely will couse such a
freezing.
 

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