【问题标题】:Queue a thread in .net在.net中排队一个线程
【发布时间】:2010-03-10 15:27:56
【问题描述】:

我有 2 个函数需要一个接一个地执行。在此函数中,进行异步调用。异步调用完成后如何执行第二个函数?

例如。

public void main()
{
   executeFn("1");
   executeFn("2"); //I want this to be executed after 1 has finished.
}


private bool executeFn(string someval)
{
     runSomeAsyncCode(); //This is some async uploading function that is yet to be defined.
}

【问题讨论】:

  • 您是在编写 runSomeAsyncCode 方法,还是来​​自第三方?
  • " 2 个函数需要一个接一个地执行" - 那不就是顺序执行吗?
  • executeFn 中的代码是线程化的,所以我需要将调用放入队列中,以便它们成为连续的。
  • 在第一次调用的异步回调中调用 executeFn("2")。
  • @SnOrfus,我们不能。当某些事件发生时,还有其他代码调用 executeFn。

标签: c# silverlight


【解决方案1】:

您可以使用Thread.Join

但是我看不到这两个函数的异步执行点,因为它们变成了顺序。

【讨论】:

    【解决方案2】:

    runSomeAsyncCode() 返回一个IAsyncResult 并实现类似于CLR Asynchronous Programming ModelBeginX EndX 方法。使用EndX方法等待代码执行完毕。

    【讨论】:

      【解决方案3】:

      您正在调用的异步方法必须在完成时通知调用者我正确吗? (否则它只是执行并忘记,这不太可能)如果是这样,您只需等待通知出现并执行第二种方法。

      【讨论】:

      • 我假设他正在寻找一种方法来发送此操作的许多执行,并且可以动态创建它们。如果是,则将它们排队等待当前操作完成
      【解决方案4】:

      试试这个:

      public void main() 
      { 
           executeFn("1"); 
           executeFn("2");
      } 
      List<string> QueuedCalls = new List<string>(); // contains the queued items
      bool isRunning = false; // indicates if there is an async operation running
      private bool executeFn(string someval) 
      { 
          if(isRunning) { QueuedCalls.Add(someval); return; } // if there is an operation running, queue the call
          else { isRunning = true; } // if there is not an operation running, then update the isRunning property and run the code
          runSomeAsyncCode(); //undefined async operation here<- 
          isRunning = false; //get here when the async is completed, (updates the app telling it this operation is done)
          if(QueuedCalls.Count != 0)//check if there is anything in the queue
          {
              //there is something in the queue, so remove it from the queue and execute it.
              string val = QueuedCalls[0];
              QueuedCalls.RemoveAt(0);
              executeFn(val);
          }
      } 
      

      这种方式将不会阻塞任何线程,并且会在第一个完成时简单地执行排队的调用,这就是我相信你想要的!快乐编码!现在 id 建议运行最后一部分,在其中将 isRunning 设置为 false,在您的异步操作中,或用事件或其他东西触发它,唯一的问题是当您的异步操作完成时必须执行一段代码,所以无论你想做什么都取决于你

      【讨论】:

      • .NET 框架为线程管理提供了许多选项,使用内置的东西更容易。我建议使用 BackgroundWorker 而不是这个 - albahari.com/threading/part3.aspx#_BackgroundWorker
      • @James Kolpack:是的,但这是对 OPS 问题的字面回答
      • 这段代码不会立即执行这两个函数吗?假设 runSomeAsyncCode() 立即返回,然后 isRunning 立即设置为 false,第二次调用则完全相同
      • @Matt No.. 因为正如我评论的那样,您将在异步操作中包含 isRunning 和检查代码,或者将其放入表明异步完成的事件中,在我的文档中明确说明了这一点
      • @Tommy,大声笑,是的,'所以无论如何你想这样做取决于你'你的评论是 OP 所要求的......而且你提供的代码具有误导性,imo
      【解决方案5】:

      您可以考虑使用通用委托执行第一个异步方法,然后在回调中执行另一个异步方法。如果您真的担心执行它们彼此之间的同步。

      【讨论】:

        【解决方案6】:

        一种简单的方法是使用自定义线程池

        http://www.codeplex.com/smartthreadpool

        你可以实例化一个单独的线程池,将线程池大小设置为1,并将workers排队

        【讨论】:

          猜你喜欢
          • 2012-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-21
          • 1970-01-01
          • 2015-11-02
          • 2021-12-31
          相关资源
          最近更新 更多