Start more than one async methods but wait previous to finish before start next

N

Nikolay Unguzov

Hi,

I want to know how to start two or more async methods, but wait each one to
complete, before proceed to the next one. My goal is to do many
time-consuming tasks one after other but not block UI.

I can use Application.DoEvents(), but is this the only solution?

Nikolay Unguzov

Sample code:

namespace AsyncTestnew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Slow1Delegate del = new Slow1Delegate(Slow1);
IAsyncResult res1 = del.BeginInvoke(null, null);

while (res1.IsCompleted == false)
{
Application.DoEvents();
}

del.EndInvoke(res1);
MessageBox.Show("ok - 1");

Slow2Delegate del2 = new Slow2Delegate(Slow2);
IAsyncResult res2 = del2.BeginInvoke(null, null);

while (res2.IsCompleted == false)
{
Application.DoEvents();
}

del2.EndInvoke(res2);
MessageBox.Show("ok - 2");
}

private delegate void Slow1Delegate();
private delegate void Slow2Delegate();

private void Slow1()
{
Thread.Sleep(4 * 1000);
}

private void Slow2()
{
Thread.Sleep(6 * 1000);
}

}
}
 
M

Marc Gravell

Well, the simple answer would be "on the background thread, do the
methods one-after-the-other" - made easier if you (as an example) but
the things to do into an array (etc) somewhere and just for-each.

However, this scenario may also be suited to a simple
producer-consumer queue.

Marc
 
N

Nikolay Unguzov

Hi Marc,

Do you think that it is a good idea to start new acync call in a callback
method of the previous one?
Like:

myDelegate.BeginInvoke(new AsyncCallback(myCallback) , null, null)
....
private void myCallback()
{
secondDelegate.BeginInvoke(new AsyncCallback(mySecondCallback) , null,
null)
}
.....

Nikolay Unguzov
 
N

Nicholas Paldino [.NET/C# MVP]

Nikolay,

Yes, this is completely acceptable. Just make sure that you call
EndInvoke on the first delegate in the callback so that you complete the
asynchronous call:

myDelegate.BeginInvoke(new AsyncCallback(myCallback) , null, null)

....
private void myCallback(IAsyncResult result)
{
// Complete the first call.
myDelegate.EndInvoke(result);

// Now begin the new call.
secondDelegate.BeginInvoke(new AsyncCallback(mySecondCallback) , null,
null);
}

The advantage of this is that if you have other operations that are
occuring, they have a chance to run.

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Nikolay Unguzov said:
Hi,

I want to know how to start two or more async methods, but wait each one
to complete, before proceed to the next one. My goal is to do many
time-consuming tasks one after other but not block UI.

I can use Application.DoEvents(), but is this the only solution?

I do not think that DoEvents() is the best solution (as a matter of fact you
should avoid calling it).
Think what would happen if you need to do this in a service, where u do not
have a DoEvents() method


You could create a Queue of delegates and a method that reads from that
queue and invoke the delegate.


Cheers,
Ignacio
 

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