System.Threading.Timer problem in Windows Forms (CF)

T

Tihomir Ignatov

Hello,
I have the following problem with application, using System.Threading.Timer:

My app is Windows Forms (CF) and I have a MyTimer class in different
assembly. From my MainForm I open other form (SecondForm) and start
listening for Timer events from MyTimer and when notification arrives, I
want to increment a counter and show it in myLabel.For example I'm using
this code:


public class SecondForm: System.Windows.Forms.Form
{
private Label myLabel;
....
int counter = 0;
MyTimer myt = new MyTimer();
myt.MyTimerEvent += new TimerEventHandler(MyMethod);

....

private void MyMethod(object sender, MyEventArgs e)
{
this.Invoke(new EventHandler(CounterEH));
}

....

private void CounterEH(object sender, EventArgs e)
{
myLabel.Text = ++counter.ToString();
}

}// Class ends




The problem is, that myLabel.Text not changes it's value. Control.Invoke
method doesn't work !?!?! If I close SecondForm , MyTimerEvent
continues to arrives....

Any ideas?
Thanks



Regards
Tihomir Ignatov
 
A

Alex Feinman [MVP]

When you display the second form using .ShowDialog(), the parent code enters
a modal loop and does not process events for the parent form. This is a
limitation in CF v1, which I believe to be lifted in v2 although I cannot
guarantee that.
If you display the child form using Show(), the events should be processed
normally
 
T

Tihomir Ignatov

Hello Alex!
Thanks, it works!
If I want to open SecondForm on button click on MainForm - OK! But how
can I open SecondForm on event in MainForm. For example:

public class MainForm: System.Windows.Forms.Form
{

SecondForm frm2;
MyTimer myt = new MyTimer();
myt.MyTimerEvent += new TimerEventHandler(ShowSecondForm);

.....

private void ShowSecondForm(object sender, MyEventArgs e)
{
frm2 = new SecondForm();
frm2.Show();


//****************************
// .ShowDialog() blocks events
//****************************
//frm2.ShowDialog();
//****************************
}


}// Class ends


This code not works!
Any ideas?


Thanks
Regards
Tihomir Ignatov
 

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