English 中文(简体)
WPF TPL 重新启动取消的任务
原标题:WPF TPL Restart a canceled Task

Here’s my problem: I cancel a Task with a Click Event which works fine. Now I want to restart the Task by clicking on the same start event which started the Task initially. The “error” that I get is that I get the MessageBox Information (“Stop Clicked”). So I’m “stuck” in the Cleanup Task.

How do I solve this? Help is very much appreciated.

谢谢!

我的守则如下:

public partial class MainWindow
{   CancellationTokenSource cts = new CancellationTokenSource();
    ParallelOptions po = new ParallelOptions();
}
private void Start_Click(object sender, RoutedEventArgs e)
{   var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    CancellationToken token = cts.Token;      
    ParallelOptions po = new ParallelOptions();  
    po.CancellationToken = cts.Token;
    po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;

    Task dlTask = Task.Factory.StartNew(() =>
            {   do
                {  token.ThrowIfCancellationRequested();
                   Parallel.For(0, dicQueryNoQueryURL.Count, po
                            , i =>
                            {   token.ThrowIfCancellationRequested();
                                if (!token.IsCancellationRequested){// do work
                                }
                            });
                }
                while (!token.IsCancellationRequested);
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
    dlTask.ContinueWith(
                (antecedents) =>
                {       if (token.IsCancellationRequested){
                        MessageBox.Show("Stop Clicked");
                    }
                    else 
                    {    MessageBox.Show("Signalling production end");   }                
                    dlTask.Dispose();
                }, uiScheduler);
}
private void btnStop_Click(object sender, RoutedEventArgs e){ cts.Cancel(); }
最佳回答

干.只是制造新的加原素,在你点首时产生新的“灯塔”。

private void Start_Click(object sender, RoutedEventArgs e)
{
    cts = new CancellationTokenSource();           
    var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    CancellationToken token = cts.Token;
...

在线书籍:

One cancellation token should refer to one "cancelable operation," however that operation may be implemented in your program. After the IsCancellationRequested property of the token has been set to true, it cannot be reset to false. Therefore, cancellation tokens cannot be reused after they have been canceled.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签