我有一个Azure工作者角色,它调用4个不同的服务,我希望能够在它自己的线程中运行每个服务,当一个完成时,启动它的另一个迭代。由于它们都需要花费不同的时间来运行,所以在我完成其他事件之前,我不想等待所有这些事情。我有这个按顺序调用它们
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机制,一旦完成就重复每个函数,而不用担心其他线程。
答案 0 :(得分:1)
如果我已经理解正确,您需要做的就是将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);
}
});
}