class Program
{
public delegate void takedelegate(int ms);
public static void takeMethod(int ms)
{
Thread.Sleep(ms);
}
static void Main(string[] args)
{
takedelegate dl = takeMethod;
IAsyncResult ar = dl.BeginInvoke(3000, null, null);
Console.WriteLine("开始执行");
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
if (ar.IsCompleted) //当异步调用执行完后,退出循环
{
Console.WriteLine("执行完毕 ");
break;
}
}
Console.Read();
}
} 我想问ar.IsCompleted属性,判断异步调用是否完成,BeginInvoke方法开始执行,就立即返回,此时ar.IsCompleted属性值为false,当异步调用执行完后,ar.IsCompleted属性值为true,不过,这个true值,是怎么传进for循环的啊?
{
public delegate void takedelegate(int ms);
public static void takeMethod(int ms)
{
Thread.Sleep(ms);
}
static void Main(string[] args)
{
takedelegate dl = takeMethod;
IAsyncResult ar = dl.BeginInvoke(3000, null, null);
Console.WriteLine("开始执行");
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
if (ar.IsCompleted) //当异步调用执行完后,退出循环
{
Console.WriteLine("执行完毕 ");
break;
}
}
Console.Read();
}
} 我想问ar.IsCompleted属性,判断异步调用是否完成,BeginInvoke方法开始执行,就立即返回,此时ar.IsCompleted属性值为false,当异步调用执行完后,ar.IsCompleted属性值为true,不过,这个true值,是怎么传进for循环的啊?
不过,这里的话,BeginInvoke方法从开始执行,就返回一个IAsyncResult接口,不是只有一个变量吗?
怎么理解"其中一个变量的值会随着另一个的值的改变而改变"呢?
worker.DoWork+=new DoWorkEventHandler((sender,e)=>
{
//ToDo
}); worker.ProgressChanged+= new ProgressChangedEventHandler((sender,e)=>
{
//ToDo
}); worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler((sender,e)=>
{
//ToDo
});然后在根据你得业务需要去做吧!
public class CustomClass
{
public bool IsCompleted { set; get; }
} public class MockDelegate
{
CustomClass cc;
public CustomClass Begin()
{
new Thread(_ => { Thread.Sleep(3000); End(); }).Start();
return cc = new CustomClass();
}
public void End()
{
cc.IsCompleted = true;
}
} static void Main(string[] args)
{ MockDelegate md = new MockDelegate(); CustomClass ar = md.Begin();
Console.WriteLine("开始执行");
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
if (ar.IsCompleted)
{
Console.WriteLine("执行完毕 ");
break;
}
}
Console.Read();
}
public class CustomClass
{
public bool IsCompleted { set; get; }
} public class MockDelegate
{
CustomClass cc;
public CustomClass Begin(Action dosth)
{
new Thread(_ => { dosth(); End(); }).Start();
return cc = new CustomClass();
}
public void End()
{
cc.IsCompleted = true;
}
} static void dosth(int ms)
{
Thread.Sleep(ms);
} static void Main(string[] args)
{ MockDelegate md = new MockDelegate(); CustomClass ar = md.Begin(() => dosth(3000));
Console.WriteLine("开始执行");
while (true)
{
Console.Write(".");
Thread.Sleep(50);
if (ar.IsCompleted)
{
Console.WriteLine("执行完毕 ");
break;
}
}
Console.Read();
}