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 addedcondition
的很抱歉,在编辑时忘了删除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));
});