问题描述
我想了解什么时候使用 TaskEx.Run
。我已经提供了两个code样我写了下面这产生相同的结果。我看不出就是为什么我会采取 TaskEx.RunEx
的办法,我敢肯定有一个很好的理由,是希望有人可以填补我
异步任务的DoWork(的CancellationToken cancelToken,IProgress<串GT;进度)
{
INT I = 0;
TaskEx.RunEx(异步()=>
{
而(!cancelToken.IsCancellationRequested)
{
progress.Report(我的ToString ++());
等待TaskEx.Delay(1 cancelToken);
}
},cancelToken);
}
私人无效Button_Click(对象发件人,RoutedEventArgs E)
{
如果(button.Content.ToString()==开始)
{
button.Content =停止;
cts.Dispose();
CTS =新CancellationTokenSource();
listBox.Items.Clear();
IProgress<串GT;进度=新进展<串将(S = GT;
{
listBox.Items.Add(多个);
listBox.ScrollIntoView(listBox.Items [listBox.Items.Count - 1]);
});
DoWork的(cts.Token,进度);
}
其他
{
button.Content =开始;
cts.Cancel();
}
}
我能做到像这样相同的结果。
异步任务的DoWork(的CancellationToken cancelToken)
{
INT I = 0;
而(!cancelToken.IsCancellationRequested)
{
listBox.Items.Add(我++);
listBox.ScrollIntoView(listBox.Items [listBox.Items.Count - 1]);
等待TaskEx.Delay(100,cancelToken); }
} 私人无效Button_Click(对象发件人,RoutedEventArgs E)
{
如果(button.Content.ToString()==开始)
{
button.Content =停止;
cts.Dispose();
CTS =新CancellationTokenSource();
listBox.Items.Clear();
DoWork的(cts.Token);
}
其他
{
button.Content =开始;
cts.Cancel();
}
}
使用 TaskEx.Run
当你想运行一个线程池环境同步code。
使用 TaskEx.RunEx
当你想在一个线程池环境中运行的异步code。
斯蒂芬Toub在行为相关的差异2的博客文章:
这只是你有的几个选项之一。如果你没有使用运行
/ RunEx
,那么你不应该。使用简单的异步
方法,并且只使用运行
/ RunEx
如果你需要在后台运行的东西。
I'm trying to understand when to use TaskEx.Run
. I have provided two code sample i wrote below that produce the same result. What i fail to see is why i would take the TaskEx.RunEx
approach, I'm sure there is a good reason and was hoping someone could fill me in.
async Task DoWork(CancellationToken cancelToken, IProgress<string> progress)
{
int i = 0;
TaskEx.RunEx(async () =>
{
while (!cancelToken.IsCancellationRequested)
{
progress.Report(i++.ToString());
await TaskEx.Delay(1, cancelToken);
}
}, cancelToken);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (button.Content.ToString() == "Start")
{
button.Content = "Stop";
cts.Dispose();
cts = new CancellationTokenSource();
listBox.Items.Clear();
IProgress<string> progress = new Progress<string>(s =>
{
listBox.Items.Add(s);
listBox.ScrollIntoView(listBox.Items[listBox.Items.Count - 1]);
});
DoWork(cts.Token, progress);
}
else
{
button.Content = "Start";
cts.Cancel();
}
}
I can achieve the same results like so
async Task DoWork(CancellationToken cancelToken)
{
int i = 0;
while (!cancelToken.IsCancellationRequested)
{
listBox.Items.Add(i++);
listBox.ScrollIntoView(listBox.Items[listBox.Items.Count - 1]);
await TaskEx.Delay(100, cancelToken);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (button.Content.ToString() == "Start")
{
button.Content = "Stop";
cts.Dispose();
cts = new CancellationTokenSource();
listBox.Items.Clear();
DoWork(cts.Token);
}
else
{
button.Content = "Start";
cts.Cancel();
}
}
Use TaskEx.Run
when you want to run synchronous code in a thread pool context.
Use TaskEx.RunEx
when you want to run asynchronous code in a thread pool context.
Stephen Toub has two blog posts related to the difference in behavior:
This is only one of several options you have for creating tasks. If you do not have to use Run
/RunEx
, then you should not. Use simple async
methods, and only use Run
/RunEx
if you need to run something in the background.
这篇关于何时使用TaskEx.Run与TaskEx.RunEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!