异步运行控制台任务
本文关键字:任务 控制台 运行 异步 | 更新日期: 2023-09-27 18:28:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewTask
{
class Program
{
static void Main(string[] args)
{
NewTask.Combine taskcombine = new NewTask.Combine();
ProfileClient profilesws = new ProfileClient();
var profileRecords = profilesws.GetAllProfiles();
foreach (var profile in profileRecords.ProfileRecords)
{
var testProfile = new NewTask.Profile();
testProfile.Id = profile.Id;
testProfile.Name = profile.Name;
var result = taskcombine.TestProfile(testProfile);
}
profilesws.Close();
taskcombine.Close();
}
}
}
我想要一种方法来运行这个异步。我想点击 ruslt 一旦它通过所有记录进入结果,我希望它结束任务。这将是一个 consol 应用程序,一旦结果填充了所需的记录,我希望它异步关闭应用程序。
如果TestProfile
有一个返回任务的TestProfileAsync
版本,你的代码将是
class Program
{
static void Main(string[] args)
{
NewTask.Combine taskcombine = new NewTask.Combine();
ProfileClient profilesws = new ProfileClient();
var profileRecords = profilesws.GetAllProfiles();
var tasks = new List<Task<ResultClass>>();
foreach (var profile in profileRecords.ProfileRecords)
{
var testProfile = new NewTask.Profile();
testProfile.Id = profile.Id;
testProfile.Name = profile.Name;
tasks.Add(taskcombine.TestProfileAsync(testProfile))
}
int completedIndex = Task.WaitAny(tasks.ToArray());
var result = tasks[completedIndex].Result;
profilesws.Close();
taskcombine.Close();
}
}
如果该函数没有异步版本,则需要将其包装在您自己的任务中。
tasks.Add(Task<ResultClass>.Factory.Start(() => taskcombine.TestProfile(testProfile)));
这一切都是假设taskcombine.TestProfile
线程安全的。如果它不是线程安全的,您将需要解释更多taskcombine.TestProfile
的作用以及是否可以创建多个实例
tasks.Add(Task<ResultClass>.Factory.Start(() =>
{
NewTask.Combine taskcombine = new NewTask.Combine(); //Move the declaration inside the task so a new Combine gets created per task.
return taskcombine.TestProfile(testProfile);
}));
编辑:您可以做的另一个调整是使用取消令牌,因此,如果您在某些任务开始之前已经有了结果,它们根本不会启动。
首先,使用具有签名Task<ResultClass> TestProfileAsync(NewTask.Profile a, CancllationToken token)
的异步版本的TestProfileAsync的梦想解决方案
class Program
{
static void Main(string[] args)
{
NewTask.Combine taskcombine = new NewTask.Combine();
ProfileClient profilesws = new ProfileClient();
var profileRecords = profilesws.GetAllProfiles();
var tasks = new List<Task<ResultClass>>();
var cts = new CancellationTokenSource();
var token = cts.Token;
foreach (var profile in profileRecords.ProfileRecords)
{
var testProfile = new NewTask.Profile();
testProfile.Id = profile.Id;
testProfile.Name = profile.Name;
tasks.Add(taskcombine.TestProfileAsync(testProfile, token))
}
int completedIndex = Task.WaitAny(tasks.ToArray());
//This should stop any tasks before they even start.
cts.Cancel();
var result = tasks[completedIndex].Result;
profilesws.Close();
taskcombine.Close();
}
}
如果您无权访问异步版本,下面是包含任务的代码的 4.5 版本
class Program
{
static void Main(string[] args)
{
NewTask.Combine taskcombine = new NewTask.Combine();
ProfileClient profilesws = new ProfileClient();
var profileRecords = profilesws.GetAllProfiles();
var tasks = new List<Task<ResultClass>>();
var cts = new CancellationTokenSource();
var token = cts.Token;
foreach (var profile in profileRecords.ProfileRecords)
{
var testProfile = new NewTask.Profile();
testProfile.Id = profile.Id;
testProfile.Name = profile.Name;
//If the token is canceled before the task gets to start itself it should never start and go stright to the "Canceled" state.
tasks.Add(Task.Run(() =>
{
token.ThrowIfCancellationRequested(); //In case the task started but we did get a result before the last
return taskcombine.TestProfile(testProfile); //Assumes "taskcombine.TestProfile(...)" is thread safe.
}, token));
}
var result = Task.WhenAny(tasks).Result;
//This should stop any tasks that have not spun up yet from spinning up
cts.Cancel();
profilesws.Close();
taskcombine.Close();
}
}