-3

I am working on an application (ASP.NET MVC5) which saves a pile of data to the database in one go. The method which saves the data takes time to do it and I do not want to block user interface.

Here I have created a test program which will sleep for 10 sec and I do not want to return any result from this program.

public Task SaveFunc()
{
    Thread.Sleep(10000);
    return null;
}
public void ShowFunction()
{
SaveFunc();
retrun "Your request is under process";
}

Now, how do I call SaveFunc in such a way that I do not have to wait for the result.

Praveen Rawat
  • 638
  • 1
  • 11
  • 27
  • You can use [Tasks](http://msdn.microsoft.com/en-us/library/system.threading.tasks(v=vs.110).aspx) namespace: `TaskFactory.StartNew(() => /* my async action */);` Be aware: in mvc, it will be block request anyway (request will wait for end of task). If you use mvc, you need to check async controllers. –  Nov 14 '14 at 07:27
  • 3
    [Related question](http://stackoverflow.com/q/1018610/2974754). – Yurii Nov 14 '14 at 07:28
  • *how do I call SaveFunc in such a way that I do not have to wait for the result.* What do you mean by that? do you want the query to execute in a *fire and forget* style? – Yuval Itzchakov Nov 14 '14 at 07:44
  • @YuvalItzchakov yes i want exactly the same – Praveen Rawat Nov 14 '14 at 08:28
  • Read [this](http://blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html) first. – Yuval Itzchakov Nov 14 '14 at 08:31

2 Answers2

0

This answer asumes you are using ASP.NET MVC - if this is not the case please update your question:

Since .NET 4.5.2 you can do the following:

public ActionResult ShowFunction()
{
    HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
    {
        // Some long-running job
    });
    return Content("Your request is under process");
}

If you are still on an old .NET version you can do something like:

public ActionResult ShowFunction()
{
    ThreadPool.QueueUserWorkItem(c =>
    {
        // Some long-running job
    });
    return Content("Your request is under process");
}

but the execution of ThreadPool.QueueUserWorkItem can be canceled by an AppDomain-recycle so you need to take care of such scenarios. Since .NET 4.0 you can also use Task.Factory.StartNew(() => { /*...*/ }); and get a Task to work with.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • @PatrickHofman: Because the OP asked `Async or parallel function **in mvc**`? With `MVC` I asumed ASP.NET MVC... – Christoph Fink Nov 14 '14 at 07:35
  • @PatrickHofman; Me too, but I hesitated to edit it, as it *could be* that the OP actually implemented MVC for another technology like WPF - lets see how the OP responds... – Christoph Fink Nov 14 '14 at 07:43
0

You should use the async method Task.Delay since Thead.Sleep is synchronous and blocks the current context. You also need to return a Task from the method instead of null and await the Task to wait until it ends. In the mean time, your program can run as is:

public Task SaveFunc()
{
    return Task.Delay(10000);
}

public async void ShowFunction()
{
    await SaveFunc().ConfigureAwait(false);
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325