C# 用于多任务运行的Task.run()

C# 用于多任务运行的Task.run(),c#,multiprocessing,windows-services,C#,Multiprocessing,Windows Services,我希望根据返回值同时运行多个任务。 下面是我想要的大致代码 class Program { static void tempFunc(string username,string password, ref Queue<KeyValuePair<string, string>> queue) { for(int i=0;i<10;i++) {

我希望根据返回值同时运行多个任务。 下面是我想要的大致代码

    class Program
    {
        static void tempFunc(string username,string password, ref Queue<KeyValuePair<string, string>> queue)
        {
            for(int i=0;i<10;i++)
            {
                Task.Delay(100000);
                Console.WriteLine(username + i);
            }
            queue.Enqueue(new KeyValuePair<string, string>(username, password));
        }
        static void Main(string[] args)
        {
            // A queue which is storing the username and password for different sessions and they need
            // to be passed to the method being called in Tasks.run
            Queue<KeyValuePair<string, string>> queue = new Queue<KeyValuePair<string, string>>();
            queue.Enqueue(new KeyValuePair<string, string>("user1", "password1"));
            queue.Enqueue(new KeyValuePair<string, string>("user2", "password2"));
            queue.Enqueue(new KeyValuePair<string, string>("user3", "password3"));

            while(true)
            {  // Based on the condition mentioned here(this condition is a method in real which returns a list of
               //string, which I will be passing as a parameter to the method being called in Tasks.run but I haven't put that in code here)
               if(condition)
               { 
                   if(queue.Count != 0)
                   {
                       KeyValuePair<string, string> temp = queue.Dequeue();
                       string username = temp.Key;
                       string password = temp.Value;

                       Task.Run(() => tempFunc(username, password, ref queue));
                   }
               }
            }
        }
    }
类程序
{
静态void tempFunc(字符串用户名、字符串密码、ref队列)
{

对于(inti=0;i来说,您就快到了。更改很少,您的程序将开始异步工作

我将推荐以下更改:

A) 将函数
tempFund
更改为包含
aysnc
。并且不需要将
queue
作为此函数的参数。任务完成后,您可以在调用函数中将用户添加回队列

public static async Task tempFunc(string username, string password)
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("Before work start : {0}", username + i);
        await Task.Delay(2000);  //I have reduced it. OP can use his time
        Console.WriteLine("After work complete : {0}", username + i);
    }
}
C) 将if块修改为:

  if (queue.Count != 0)
  {
      var temp = new KeyValuePair<string, string>();
      queue.TryDequeue(out temp);
      string username = temp.Key;
      string password = temp.Value;

      Task.Run(async () =>
      {
          Console.WriteLine("before Task start : {0}", username);
          await tempFunc(username, password);
          Console.WriteLine("after Task Ends : {0}", username);
          queue.Enqueue(new KeyValuePair<string, string>(username, password));
      });
if(queue.Count!=0)
{
var temp=新的KeyValuePair();
queue.TryDequeue(out temp);
字符串用户名=临时密钥;
字符串密码=临时值;
Task.Run(异步()=>
{
WriteLine(“任务开始前:{0}”,用户名);
等待tempFunc(用户名、密码);
WriteLine(“任务结束后:{0}”,用户名);
排队(新的KeyValuePair(用户名、密码));
});

记下额外的日志,这将使程序明显处于异步运行状态。

如果要以并行方式使用队列,您可能需要使用
ConcurrentQueue
此代码有太多的编译器错误,我无法弄清楚您是如何运行itI added
condition
的很抱歉,在编辑时忘了删除
wait
。现在删除了它。你的代码中有太多问题。我猜你不熟悉多线程和TPL。试着阅读并获取有关主题的信息:线程、任务、,Async/Await、线程安全、并发收集在你发布后我读了一点关于它的内容。但是我对它的工作方式有点困惑。当我调用方法
Await
时,主线程不会等待方法返回并将继续。但是如果不是
Await Task.Delay(2000)
我只想运行一些语句,我该怎么做?因为没有
await
就不能使用
async
。编辑-当我在方法上调用
await
时,主线程将等待,直到调用的方法中遇到
await
并将继续。但是如果我根本不希望主线程等待,则正确的方法是将延迟添加为
0
而不是
2000
,并将该语句放在函数的开头?因为我只希望主线程在调用该方法后继续运行,而不必等待任何时间。@sporty_1993如果在一个方法上调用
wait
则不意味着
main
线程将等待它。在您的情况下,一个新的
任务
已经启动,调用
tempFunc
。因此,即使是新任务也不会在
等待
时等待
,它将变为空闲。在
等待
之后的行被调用,就像在
委托
中一样,只要
tempFunc
完成。我代表
任务。延迟
,就好像要做一些工作一样。任何你想做的计算或其他工作,然后删除
Task.Delay
并添加新行。如果我没有放入
Task.Delay
并替换为'wait Task.Delay(2000)59449要替换
任务。使用
while循环
for循环
延迟
,或者不需要
等待
语句块。
  if (queue.Count != 0)
  {
      var temp = new KeyValuePair<string, string>();
      queue.TryDequeue(out temp);
      string username = temp.Key;
      string password = temp.Value;

      Task.Run(async () =>
      {
          Console.WriteLine("before Task start : {0}", username);
          await tempFunc(username, password);
          Console.WriteLine("after Task Ends : {0}", username);
          queue.Enqueue(new KeyValuePair<string, string>(username, password));
      });