与Task.Run有区别的是void方法和返回null的Task方法吗?

时间:2017-02-13 13:14:09

标签: c# task

考虑一个包含2个按钮和richtextbox的表单:

public partial class MainForm : Form
{
    CancellationTokenSource cts;
    CancellationToken token;

    public MainForm()
    {
        InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        cts = new CancellationTokenSource();
        token = cts.Token;
        var task = Task.Run(() => WriteSomeLines(), token);
    }

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        cts.Dispose();
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        cts = new CancellationTokenSource();
        token = cts.Token;
        var task = Task.Run(() => WriteSomeLines(), token);
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        try
        {
            cts.Cancel();
            cts.Dispose();
        }
        catch (ObjectDisposedException exc)
        {
            MessageBox.Show(exc.GetType().Name);
            //object disposed
        }
    }

    public void WriteSomeLines()
    {
        if (ControlInvokeRequired(rtbLoops, () => rtbLoops.Text += "Starting new loop \r\n")) ;
        else rtbLoops.Text += "Starting new loop \r\n";
        for (int i = 0; i < 30; i++)
        {
            try
            {
                if (ControlInvokeRequired(rtbLoops, () => { rtbLoops.AppendText("New line " + i + "\r\n"); rtbLoops.ScrollToCaret(); })) ;
                else rtbLoops.AppendText("New line " + i + "\r\n");

                Thread.Sleep(250);
                token.ThrowIfCancellationRequested();
            }
            catch (OperationCanceledException ae)
            {
                MessageBox.Show(ae.GetType().Name);
                return;
            }
        }
        return;
    }

    public bool ControlInvokeRequired(Control c, Action a)
    {
        if (c.InvokeRequired)
            c.Invoke(new MethodInvoker(delegate { a(); }));
        else
            return false;

        return true;
    }      
}

如果WriteSomeLines()返回void并且我在内部使用return,或者如果WriteSomeLines()返回Task并且我在那里返回null,那么会有区别吗?我读到我不能将await与void返回方法一起使用,而是插入

await task;

在任务声明之后(在上面的代码中)编译完全正常,并且运行没有问题。

编辑:

private async void btnStart_Click(object sender, EventArgs e)
    {
        cts = new CancellationTokenSource();
        token = cts.Token;
        var task = Task.Run(() => WriteSomeLines(), token);
        await task;
        rtbLoops.Text += "Task complete";
    }

如果WriteSomeLines()返回void,则编译没有问题。

另外,稍微没有实现,我在这里正确处理CancellationTokenSource吗?

第二次编辑:

这是正确的做法:

 private async void btnStart_Click(object sender, EventArgs e)
    {
        cts.Dispose();
        cts = new CancellationTokenSource();
        token = cts.Token;
        var task = Task.Run(() => WriteSomeLines(), token);
        bool result = await task;
        if(result == true) rtbLoops.Text += "Task complete \r\n";
    }

public async Task<bool> WriteSomeLines()
    {
        if (ControlInvokeRequired(rtbLoops, () => rtbLoops.Text += "Starting new loop \r\n")) ;
        else rtbLoops.Text += "Starting new loop \r\n";
        for (int i = 0; i < 30; i++)
        {
            try
            {
                if (ControlInvokeRequired(rtbLoops, () => { rtbLoops.AppendText("New line " + i + "\r\n"); rtbLoops.ScrollToCaret(); })) ;
                else rtbLoops.AppendText("New line " + i + "\r\n");
                await Task.Delay(250);
                token.ThrowIfCancellationRequested();
            }
            catch (OperationCanceledException ae)
            {
                MessageBox.Show(ae.GetType().Name);
                return false;
            }
        }
        return true;

1 个答案:

答案 0 :(得分:4)

你永远不应该返回空任务;这应该会导致运行时NullReferenceException错误。

您可以在 await方法中使用async void ,但不能使用await 使用 {{1}方法(因为你不能async void await)。

我建议您查看我的Request an authorization code;它应该有助于您更好地了解voidasync

  

我在这里正确处理CancellationTokenSource吗?

您的开始按钮需要在创建新的await时取消/处置旧的cts