Delegates & Asynchronous Callback

S

standogan

I have a long running process, and I use Delegates to start it, and
during the process I want to show a wait form to the user.

///////////////////CODE/////////////////////////////////

public void StartProcess()
{
MyDelegate del = new MyDelegate(LongRunningProcess);
del.Invoke(new AsyncCallback(CloseWaitForm), null);

waitForm.ShowDialog(this);
}

public void CloseWaitForm(IAsyncResult result)
{
waitForm.HideYourself();
Application.DoEvents();

AsyncResult ar = (AsyncResult) result;
MyDelegate del = (MyDelegate)ar.AsyncDelegate;
del.EndInvoke(ar);
}

////////////////////////////////////////////////////////

If in the LongRunningProcess() an exception occurs, and invoke ends
(CloseWaitForm calls), can it be before the line in which I show the
form? I mean, invoke ends and waitForm hides, before it is shown, then
it shows up and stays there forever. Is this a possibility? If it is
how I can avoid it?

Best Regards...
 
G

Guest

Technically speaking, yes it could. This is because the "LongRunningProcess"
is running on a different thread and the asynchronous callback will happen on
the same thread that the "LongRunningProcess" is running on.

If the system changed threads after your call to invoke it is conceivable
that the callback could happen before the call to ShowDialog. It's very
unlikely but if it did happen it would be very hard to debug.
 
W

Willy Denoyette [MVP]

I have a long running process, and I use Delegates to start it, and
during the process I want to show a wait form to the user.

///////////////////CODE/////////////////////////////////

public void StartProcess()
{
MyDelegate del = new MyDelegate(LongRunningProcess);
del.Invoke(new AsyncCallback(CloseWaitForm), null);

waitForm.ShowDialog(this);
}

public void CloseWaitForm(IAsyncResult result)
{
waitForm.HideYourself();
Application.DoEvents();

AsyncResult ar = (AsyncResult) result;
MyDelegate del = (MyDelegate)ar.AsyncDelegate;
del.EndInvoke(ar);
}

////////////////////////////////////////////////////////

If in the LongRunningProcess() an exception occurs, and invoke ends
(CloseWaitForm calls), can it be before the line in which I show the
form? I mean, invoke ends and waitForm hides, before it is shown, then
it shows up and stays there forever. Is this a possibility? If it is
how I can avoid it?

Best Regards...

Hmm...
A delegates Invoke doesn't take an AsynchCallBack delegate as argument, are
you sure this is the code you are compiling or am I missing something??

del.Invoke(new AsyncCallback(CloseWaitForm), null);


Willy.
 

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