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);
}
}
}
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);
}
}
}