对于在以下情况下如何实现取消标记,我有些困惑。
说我有一个方法,它有一个取消令牌,没有指定超时,像这样。
public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
Task.Delay(1000, cancellationToken)
}
catch (OperationCanceledException canceledException)
{
// Do something with canceledException
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", canceledException);
throw;
}
catch (Exception exception)
{
// Do something with exception
Console.WriteLine("DoSomeElseAsyncThingAsync {0}", exception);
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
但是在该方法中,我想调用另一个期望CancellationToken除此以外的方法,但这次我要对此设置超时。
public static async Task DoSomeAsyncThingAsync(CancellationToken cancellationToken = default)
{
try
{
var innerCancellationTokenSource = new CancellationTokenSource();
innerCancellationTokenSource.CancelAfter(1000);
var innerCancellationToken = innerCancellationTokenSource.Token;
await DoSomeElseAsyncThingAsync(innerCancellationToken);
}
catch (OperationCanceledException canceledException)
{
// Do something with …Run Code Online (Sandbox Code Playgroud) c# cancellation async-await cancellationtokensource cancellation-token
我不完全理解取消令牌,但我相信这是我需要使用的。
我有一个充满文件路径的列表框,以及一个方法 ( ProcesstListSort),它遍历列表框中的每个文件路径并根据文件类型执行不同的方法。ProcessListSort从另一个方法调用,即从按钮单击调用。我试图BeginEverything在后台任务中运行,因为它锁定了 UI。在这种情况下实现取消令牌检查的最佳位置是什么?
单击此按钮开始该过程:
public async void button1_Click(object sender, EventArgs e)
{
Task task1 = new Task(BeginEverything);
task1.Start();
await task1;
}
Run Code Online (Sandbox Code Playgroud)
哪个启动这个:
public void BeginEverything()
{
CreateThing1();
CreateThing2();
ProcessListSort(); //This is the one I think I need to interrupt because it's the longest
CreateThing3();
CreateThing4();
}
Run Code Online (Sandbox Code Playgroud)
在这里启动最长的任务(根据文件类型对文件进行排序和执行其他方法,将文件路径传递给其他方法):
public void ProcessListSort()
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
string p = listBox2.Items[i].ToString();
FileAttributes attr = File.GetAttributes(p);
if (p.EndsWith(".zip"))
{
Method1(p); …Run Code Online (Sandbox Code Playgroud) 我想取消一个线程,然后再运行另一个线程.这是我的代码:
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation token
new Thread(() => WaveFormLoop(cancelWaveForm.Token)).Start(); // starting a new thread
}
Run Code Online (Sandbox Code Playgroud)
当我调用这个方法时,第一个线程不会停止而第二个线程开始运行...
但是如果我跳过最后两行它会工作:
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
//cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation …Run Code Online (Sandbox Code Playgroud) c# multithreading cancellationtokensource cancellation-token
嗨,我一直在论坛上阅读了很多,但我无法找到问题的答案...
这是我想在布尔值变为 TRUE 时取消的函数:
Task<PortalODataContext> task = Task.Factory.StartNew(() =>
{
var context = connection.ConnectToPortal();
connection.ListTemplateLib = this.ShellModel.ConnectionManager.GetTemplateLibrarys(connection);
connection.ListTemplateGrp = this.ShellModel.ConnectionManager.GetTemplateGroups(connection, connection.TemplateLibraryId);
connection.ListTemplates = this.ShellModel.ConnectionManager.GetTemplates(connection, connection.TemplateGroupId);
return context;
}, token);
Run Code Online (Sandbox Code Playgroud)
如何在没有 LOOP 的情况下验证令牌是否收到取消请求?
类似的东西:
if (token.IsCancellationRequested)
{
Console.WriteLine("Cancelled before long running task started");
return;
}
for (int i = 0; i <= 100; i++)
{
//My operation
if (token.IsCancellationRequested)
{
Console.WriteLine("Cancelled");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我没有需要循环的操作,所以我不知道该怎么做......