簡體   English   中英

暫停Foreach循環,直到第一個迭代完成

[英]Pause Foreach Loop until the first Iteration is completed

這是我的情況:

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嗎? 或者,還有更好的方法?

您可以使用事件處理程序對其進行同步

ClassA
{
  ManualResetEvent mre = new ManualResetEvent(false);
  Timer tmr = new Timer(1000);
  void Start()
     {
       tmr.start();
     }

  void tmr_Elapsed(object sender, ElapsedEventArgs e)
        {

            x++;
            if (x > 50)
            {
                //Do Something

                tmr.Stop(); // Or Change Value of a Property! anything that shows it
                            //meets the condition.
                mre.Set();
            }
        }

  public void Wait()
  { 
     mre.WaitOne();
  }
}


Class WorkflowController
{
   list<ClassA> allA=new list<ClassA>(){new A1,new A2, new A3}
   void Start()
   {
     foreach(item in allA)
     {
        item.start();
        item.Wait();
     } 
   }
}

您可以將Tasks與以下實現一起使用:

List<ClassA> all = new List<ClassA> { ... };
private async void Start()
{
    foreach (var item in all)
    {
         await item.DoWork();
    }
}

public class ClassA
{
    public async Task DoWork()
    {
        Thread.Sleep(50000); // wait 50sec
        // Do work            
    }
}

在此處“如何:等待一個或多個任務完成”

希望對您有所幫助!

private async void Start()
{
    foreach (var item in all)
    {
         await item.DoWorkAsync();
    }
}

public class ClassA
{
    public async Task DoWorkAsync()
    {
        bool done = false;

        while(!done)
        {
            DoStuff();
            DoMoreStuff();

            if(someCondition)
            {
                done = true;
            }
            else
            {
                await Task.Delay(1000);
            }
        }
    }
}

實際上,您的代碼似乎沒有什么不同。

for(var i = 0; i < 3; i++)
{
    Thread.Sleep(1000)
    Task.Wait(Task.StartNew(() =>
        {
            //Do Something
            if (myCondition==true)
            {
                // Do Something
                // Or Change Value of a Property! anything that shows it
                // meets the condition.
            }
        });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM