Start may not be called on a promise-style task. exception is coming

Related searches

I am creating a simple wpf desktop application. UI have just a button and code in .cs file like.

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    FunctionA();
}

public void FunctionA()
{
    Task.Delay(5000).Start();
    MessageBox.Show("Waiting Complete");
}

But surprisingly line Task.Delay(5000).Start(); is throwing an InvalidOperationException:

Start may not be called on a promise-style task.

Can any one help why it is like this?

You are getting that error because the Task class already started the task before giving it to you. You should only ever call Start on a task that you create by calling its constructor, and you shouldn't even do that unless you have a compelling reason to not start the task when you create it; if you want it started right away you should use Task.Run or Task.Factory.StartNew to both create and start a new Task.

So, now we know to just get rid of that pesky Start. You'll run your code and find that the message box is shown right away, not 5 seconds later, what's up with that?

Well, Task.Delay just gives you a task that will be completed in 5 seconds. It doesn't stop execution of the thread for 5 seconds. What you want to do is have some code that's executed after that task finishes. That's what ContinueWith is for. It lets you run some code after a given task is done:

public void FunctionA()
{
    Task.Delay(5000)
    .ContinueWith(t => 
    {
        MessageBox.Show("Waiting Complete");
    });
}

This will behave as expected.

We could also leverage C# 5.0's await keyword to add continuations more easily:

public async Task FunctionA()
{
    await Task.Delay(5000);
    MessageBox.Show("Waiting Complete");
}

While a full explanation of what's going on here is beyond the scope of this question, the end result is a method that behaves very similar to the previous method; it will show a message box 5 seconds after you call the method, but the method itself will return [almost] right away in both cases. That said, await is very powerful, and allows us to write methods that seem simple and straightforward, but that would be much harder and messier to write using ContinueWith directly. It also greatly simplifies dealing with error handling, taking out a lot of boilerplate code.

Start may not be called on a promise-style task. exception is coming , Start() gives the "Start may not be called on a promise-style task"-error. I searched online and what I got out of it is that I can't call .Start() because´┐Ż Start may not be called on a promise-style task. exception is coming (2) As Servy said, the task has already started, so all you have left to do is wait for it (.Wait ()): private void Button_Click_2(object sender, RoutedEventArgs e) { FunctionA (); } public void FunctionA() { Task.Delay ( 5000 ).Wait (); MessageBox.Show ( "Waiting Complete" ); }

Try this.

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    FunctionA();
}

public async void FunctionA()
{
    await Task.Delay(5000);
    MessageBox.Show("Waiting Complete");
}

Problem with Task.Start() - MSDN, Start may not be called on a promise-style task. Moving the async call to Task. Here is the working code to avoid the above exception Task Parallel Library: Start may not be called on a promise-style task

As Servy said, the task has already started, so all you have left to do is wait for it (.Wait()):

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    FunctionA();
}
public void FunctionA()
{
    Task.Delay(5000).Wait();
    MessageBox.Show("Waiting Complete");
}

Start may not be called on a promise-style task., Wait(); } This throws InvalidOperationException: Start may not be called on a promise-style task. Version 3: Hmm.. Task.RunSynchronously sounds promising. 171 Start may not be called on a promise-style task. exception is coming Mar 1 '13 154 Create a completed Task Jan 9 '13 117 Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' Feb 2 '13

Synchronously waiting for an async operation, and why does Wait(), InvalidOperationException was unhandled by user code HResult=-2146233079. Message=Start may not be called on a promise-style task. If Task.WaitAll() is not returning at all, it may be worth digging into the DataAccess methods, as perhaps one of those calls is actually never completing? Marked as answer by DotNetNub Tuesday, November 15, 2016 4:02 PM

Getting "RunSynchronously may not be called on a continuation task , A task may be started and run only once. Any attempts to schedule a task a second time will result in an exception. The Start is used to execute a task that has been created by calling one of the Task constructors. Typically, you do this when you need to separate the task's creation from its execution, such as when you conditionally execute

This throws InvalidOperationException: RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method. Version 4 (the solution): private void WriteToLog(string text) { var task = Task.Run(async => { await WriteToLogAsync(text); }); task.Wait(); } This works.

Comments
  • Calling Wait() on a task will block the current thread until the task resolves. That's almost never what you want to happen.
  • @Jeremy: Indeed, it's worth paying attention to the behaviour that you mentioned, but int this case his FunctionA was already blocking the current thread, so I assumed he's just looking for a way to determine when the task has completed. To clarify the difference between Wait and async (for future readers) please read this link