简体   繁体   中英

Is there a difference with Task.Run a void method and Task method returning null?

Consider a form with 2 buttons and a 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;
    }      
}

Is there a difference if WriteSomeLines() is returning void and I use return inside, or if WriteSomeLines() returns Task and I do return null there? I read that I cannot use await with void returning methods, but inserting

await task;

after task declaration (in the code above) compiles perfectly fine, and runs with no issues.

Edit:

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";
    }

This compiles with no issues if WriteSomeLines() returns void.

Also, slightly unrealted, am I disposing CancellationTokenSource correctly here?

Second Edit:

So is this the correct approach:

 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";
    }

and

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;

You should never return a null task; that should cause a runtime NullReferenceException error.

You can use await within an async void method, but you cannot use await to consume an async void method (because you cannot await void ).

I recommend that you review my async intro blog post ; it should help you get a better understanding of async and await .

am I disposing CancellationTokenSource correctly here?

Your start button needs to cancel/dispose the old cts when it creates a new one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM