I have an interesting problem..
I have a Form that launches another Form (2) through a Button. Before Form2 Closes, it sometimes fires an Event which Invalidates Form 1 and forces Form 1 to refresh it’s data. The problem I have is After Form 2 fires the event, Form 1 seems to get it and handles it, and refreshes it’s data and Only then Form 2 Closes. I want Form 2 to Fire the event and Close, BEFORE Form1’s event handler catches and processes the event.
I have a feeling it is related to BackgroundWorker (sort of like SwingUtilities.InvokeLater in Java) .. but I am not that experienced with it ..
public class Frm1{
void LaunchForm2(){
Frm2 form2 = new Frm2();
form2.dataChanged += new DataChangeListener(myListener);
form2.showDialog();
}
private void myListener(){
//get my data again
}
}
public class Frm2{
private void Close(){
if(myDataHasChanged){
if(dataChanged != null) {
dataChanged();
}
this.Close();
}
}
}
You can raise the event after calling
Close– callingForm.Closewill not exit the method that called it.EDIT: Try using
BeginInvoketo wait for the next message loop before handling the event, like this:2nd EDIT: To give Form1 a chance to repaint, call BeginInvoke twice to wait two message loops before updating (One to close Form2, and a second to repaint Form1), like this: