【发布时间】:2013-07-09 21:04:51
【问题描述】:
这是我的场景:
ClassA
{
Timer tmr = new Timer(1000);
void Start()
{
tmr.start();
}
void tmr_Elapsed(object sender, ElapsedEventArgs e)
{
//Do Something
if (myCondition==true)
{
//Do Something
tmr.Stop(); // Or Change Value of a Property! anything that shows it
//meets the condition.
}
}
}
Class WorkflowController
{
list<ClassA> allA=new list<ClassA>(){new A1,new A2, new A3}
void Start()
{
foreach(item in allA)
{
item.start()
}
}
}
问题:
现在我希望 foreach loop 执行 A1.Start() 并等到计时器满足条件(此处为 x>50 并在 50 秒后)并停止。然后执行A2.Start() 并等待计时器满足条件并再次停止。然后执行A3.Start() 等等。 WorkFlowController 控制我的应用程序的工作流程。我不知道做这件事的简单方法是什么。我应该使用INotifyPropertyChanged 吗?我应该使用Eventhandler 吗?还是有更好的方法?
【问题讨论】:
-
我希望这只是示例代码。对于计时(等待 X 秒),不要使用计时器,而是使用
StopWatch类。要在执行任何异步操作时暂停代码执行,请使用WaitHandle。 -
考虑使用手动重置事件
-
呃,你是在用异步代码做同步的吗?
-
您似乎正在尝试重新实现
Task。这一切都已经为你完成了。 msdn.microsoft.com/en-us/library/dd537609.aspx
标签: c# .net multithreading async-await