background worker and win forms

  • Thread starter Thread starter bilosta
  • Start date Start date
B

bilosta

I need help with background worker and form

I have a class (form1) that works with database. I need this:
-Show form2 while form1 is geting data from database
-When form1 is done, close form2, proced working form1

I try these things with thread but a can not do it
I try it with background worker, but I
dont know how:(



does enyone knows how to do this thing.

Thanx in advance

Oga
 
Hi Oga,

It sounds like the piece you are missing is something like this....

When the Background Thread (the database work) in 'form1' is done, you
will need to fire an event. First, create a delegate to handle the
closing of 'form2' by a Background Thread, and the Event that will be
fired:

public delegate void Form1DoneDelegate();
public event Form1DoneDelegate WorkFinishedEvent;

Then, subscribe to that Event in 'form1':

Form1.WorkFinishedEvent+= new Form1DoneDelegate (CloseForm2);


Finally, create the 'CloseForm2' method (it needs to close 'form2', or
whatever) - something like this:

private void CloseForm2()
{
if (Form2.InvokeRequired) //Make sure we are on the main UI
thread
{
BeginInvoke(new Form1DoneDelegate(CloseForm2));
}
else
{
Form2.Close();
}
}

Note that the number of arguments for the delegate (Form1DoneDelegate)
and method (CloseForm2) match - they are zero. You can add some if you
need to.


Hope this helps.
Dave
 
Back
Top