C# 带While循环的多线程处理
我有一个Azure Worker角色,它调用4个不同的服务,我希望能够在它自己的线程中运行每个服务,当其中一个完成时,启动它的另一个迭代。由于它们都需要不同的时间运行,我不想在一个事件完成后开始另一个事件之前等待它们。我有一个顺序调用它们的程序C# 带While循环的多线程处理,c#,asynchronous,async-await,C#,Asynchronous,Async Await,我有一个Azure Worker角色,它调用4个不同的服务,我希望能够在它自己的线程中运行每个服务,当其中一个完成时,启动它的另一个迭代。由于它们都需要不同的时间运行,我不想在一个事件完成后开始另一个事件之前等待它们。我有一个顺序调用它们的程序 public class WorkerRole : RoleEntryPoint { private readonly CancellationTokenSource cancellationTokenSource = new Cancellat
public class WorkerRole : RoleEntryPoint
{
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
public override void Run()
{
Trace.TraceInformation("Polling.Worker is running");
try
{
this.RunAsync(this.cancellationTokenSource.Token).Wait();
}
finally
{
this.runCompleteEvent.Set();
}
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
bool result = base.OnStart();
Trace.TraceInformation("Polling.Worker has been started");
return result;
}
public override void OnStop()
{
Trace.TraceInformation("Polling.Worker is stopping");
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
base.OnStop();
Trace.TraceInformation("Polling.Worker has stopped");
}
private async Task RunAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Run(() =>
{
Debug.WriteLine("Starting Reddit Service");
RedditService.GetObjects();
Debug.WriteLine("Completed Reddit Service");
});
await Task.Run(() =>
{
Debug.WriteLine("Starting TV Show Service");
TVShowTicketService.GetObjects();
Debug.WriteLine("Completed TV Show Service");
});
await Task.Run(() =>
{
Debug.WriteLine("Starting Play By Play Service");
PlayByPlayService.GetObjects();
Debug.WriteLine("Completed Play By Play Service");
});
await Task.Run(() =>
{
Debug.WriteLine("Starting Profile Service");
ProfileService.Main();
Debug.WriteLine("Completed Profile Service");
});
await Task.Delay(1000);
}
}
}
我知道我正在等待每个线程,我希望基本上能够有一些while机制,在每个函数完成后重复执行,而不必担心其他线程。如果我理解正确,您所需要做的就是将
while
循环移动到每个任务中:
private async Task RunAsync(CancellationToken cancellationToken)
{
await Task.WhenAll(
Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
Debug.WriteLine("Starting Reddit Service");
RedditService.GetObjects();
Debug.WriteLine("Completed Reddit Service");
await Task.Delay(1000);
}
}),
Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
Debug.WriteLine("Starting TV Show Service");
TVShowTicketService.GetObjects();
Debug.WriteLine("Completed TV Show Service");
await Task.Delay(1000);
}
}),
Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
Debug.WriteLine("Starting Play By Play Service");
PlayByPlayService.GetObjects();
Debug.WriteLine("Completed Play By Play Service");
await Task.Delay(1000);
}
}),
Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
Debug.WriteLine("Starting Profile Service");
ProfileService.Main();
Debug.WriteLine("Completed Profile Service");
await Task.Delay(1000);
}
}));
}
通过将重复的元素封装在助手方法中,可以提高可读性和可维护性:
private async Task RunAsync(CancellationToken cancellationToken)
{
await Task.WhenAll(
RunServiceAsync(cancellationToken, RedditService.GetObjects, "Reddit"),
RunServiceAsync(cancellationToken, TVShowTicketService.GetObjects, "TV Show"),
RunServiceAsync(cancellationToken, PlayByPlayService.GetObjects, "Play By Play"),
RunServiceAsync(cancellationToken, ProfileService.Main, "Profile"));
}
Task RunServiceAsync(CancellationToken cancellationToken, Action service, string description)
{
return Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
Debug.WriteLine("Starting " + description + " Service");
service();
Debug.WriteLine("Completed " + description + " Service");
await Task.Delay(1000);
}
});
}
你的目标不明确。为什么不在一个循环的长时间运行的任务中运行每个服务?既然您(似乎)只想重复运行每个服务,为什么角色必须管理每个服务的迭代?“一些while机制,在每个函数完成后只重复它”——对我来说听起来像是一个
while
循环,每个服务一个,在每个服务的任务中。请解释为什么这对您不起作用。我不同意,所以您建议每个服务包含一个单独的循环,或者每个服务包含一个任务,每个任务中包含一个循环?也许一个小样本会有所帮助?我已经添加了一个答案,我希望代码能够解决所描述的问题。我意识到这在你的场景中可能不起作用(因为假设你会这么做),所以如果答案没有帮助,请编辑你的问题,提供一个好的、清晰的解释,解释为什么将而移动到每个任务中的简单方法在你的情况下不起作用。哇,这正是我想要的。我不太清楚为什么我认为这会更加困难。谢谢