0

这基本上是代码:

private void TaskGestioneCartelle()
{
    Task.Factory.StartNew(() => GeneraListaCartelle())
        .ContinueWith(t => GeneraListaCartelleCompletata()
        , CancellationToken.None
        , TaskContinuationOptions.None
        , TaskScheduler.FromCurrentSynchronizationContext());
}

private void GeneraListaCartelle()
{
    // ... code
}

private void GeneraListaCartelleCompletata()
{
    Task.Factory.StartNew(() => CopiaCartelle())
        .ContinueWith(t => CopiaCartelleCompletato()
        , CancellationToken.None
        , TaskContinuationOptions.None
        , TaskScheduler.FromCurrentSynchronizationContext());
}

private void CopiaCartelle()
{
    // long operation...
}

事实上,当 CopiaCartelle 启动时,我并没有进入新线程,因为它需要很长时间,并且 UI 完全冻结(而 on GeneraListaCartelle(),这也需要很长时间,这不会发生)。也因为我可以在 UI 中的控件上编写而不使用InvokeRequiredand MethodInvoker

我错过了一些观点?

4

2 回答 2

3

尝试更改 Task.Factory.StartNew(() => CopiaCartelle()) 为以下内容:

Task.Factory.StartNew(() => CopiaCartelle(),  CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default))

您将在 UI 线程上继续进入 GeneraListaCartelleCompletata,它似乎在 UI 线程上调度任务 - 放置 TaskScheduler.Default 将使其在自己的线程中运行。(刚刚测试了这个以确认)

于 2013-04-18T11:06:46.320 回答
0

@NDJ 发布的内容是正确的,我做了一个简单的例子来展示发生了什么。

首先,方法:

private static void TaskGestioneCartelle()
{            
    Task.Factory.StartNew(() => GeneraListaCartelle())
        .ContinueWith(t => GeneraListaCartelleCompletata()
        , CancellationToken.None
        , TaskContinuationOptions.None
        , TaskScheduler.FromCurrentSynchronizationContext());
}

private static void GeneraListaCartelle()
{
    //No sleep could block the thread UI because the task is being executed on a different Thread
    Debug.WriteLine("GeneraListaCartelle " + Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(4000);            
    mainForm.Invoke(new Action(() => bla.Text = "uno due tre, Genera Lista!"));            
}

private static void GeneraListaCartelleCompletata()
{
    //This is begin executed on the UI thread
    Debug.WriteLine("GeneraListaCartelleCompletata " + Thread.CurrentThread.ManagedThreadId);
    Task.Factory.StartNew(() => CopiaCartelle())
        .ContinueWith(t => CopiaCartelleCompletato()
        , CancellationToken.None
        , TaskContinuationOptions.None
        , TaskScheduler.FromCurrentSynchronizationContext());
}

private static void CopiaCartelle()
{
    //This is begin executed on the UI thread (doesn't even show in the form 'cause the thread is blocked)
    Debug.WriteLine("CopiaCartelle " + Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(4000);   
    mainForm.Invoke(new Action(() => bla.Text = "Copia Cartelle \\o"));            
}

private static void CopiaCartelleCompletato()
{
    //This is begin executed on the UI thread
    Debug.WriteLine("CopiaCartelleCompletato " + Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(4000);   
    mainForm.Invoke(new Action(() => bla.Text = "Completato!"));
    //Stops blocking the UI thread
}

现在表单和组件

    static Label bla = new Label()
    {
        Text = "Mama Mia, Gestione Cartelle!",
        Left = 100,
        Top = 100,
        Width=300
    };

    static Label hangOn = new Label()
    {
        Text="Hang loose"
    };

    static Form mainForm = new Form()
    {
        Width = 600,
        Height = 600
    };

[STAThread]
static void Main(string[] args)
{
    mainForm.Controls.Add(bla);
    mainForm.Controls.Add(hangOn);
    mainForm.MouseMove += (o, e) => { hangOn.Left = e.X; hangOn.Top = e.Y; };
    Debug.WriteLine("UI Thread: "+ Thread.CurrentThread.ManagedThreadId);
    TaskGestioneCartelle();

    Application.Run(mainForm);
}

首先,运行应用程序并继续移动鼠标。Hang Loose当Label 停止跟随您的鼠标时,您会注意到 UI 线程被阻止。

现在,如果你Output从 Debug 中检查,你会看到类似这样的东西:

UI Thread: 10
GeneraListaCartelle 6
GeneraListaCartelleCompletata 10
CopiaCartelle 10
CopiaCartelleCompletato 10

看?它使用 UI 线程来运行您的任务,从而挂起您的 UI。

现在,TaskScheduler.FormCurrentSynchronizationContext()改为TaskScheduler.Default

多田:

UI Thread: 8
GeneraListaCartelle 9
GeneraListaCartelleCompletata 10
CopiaCartelle 10
CopiaCartelleCompletato 11

在注释中,看到我mainForm.Invoke用来调用要在 中执行的操作UI Thread,例如更改标签文本。

如果您还有任何疑问,请随时发表评论。

于 2013-04-18T13:25:08.183 回答