0

I have a service solution. (Console executable)

When the request comes from UI, it has to run a couple of operations.

First, based on the request that came from UI, it will fetch records from DB (The records can be in millions)

Once the records are available, there is a whole bunch of code that processes each record and do some publishing.

The UI will not wait for the whole operation to be completed.

UI should make a call and then the whole lengthy process should run in the background, meanwhile, the service will immediately send a response back to UI

I tried to put the whole long-running code (Fetching of records, processing and publishing) in a separate method.

I am doing like

Method()
{
    try
    {
        BackgroundProcessing();
        return true;
    }
    Catch (Exception ex)
    {
        // logging of exception
        return false;
    }

}

async void BackgroundProcessing()
{
    await Task.Run( () =>
    {
            // Fetching of records
           // Processing of records
    });
}
user3458219
  • 141
  • 1
  • 4
  • 11
  • Yes potentially, `await Task.Run(Method)`, which will potentially start a new thread, and give you a continuation back on the UI thread. There are only few use cases for `Task.Factory.StartNew` in modern .net, where you know better and would like to use explicit options not available in `Task.Run`, and additionally are aware of the problems it can cause. – TheGeneral Sep 14 '20 at 04:05
  • If this operation works purely with database data, then you'll probably find that it's much quicker to execute it in the database. – Nick.Mc Sep 14 '20 at 04:12
  • @ Michael Randall: I have edited the question. Are you suggesting that way? My only concern is whether this will support the long-running process. @Nick: Anyway, I need to get those data from Db to service for individual processing. – user3458219 Sep 14 '20 at 04:36
  • Yes it will, however it will tie up a thread-pool thread using `Task.Run`. You could use the `LongRunning` hint to suggest that it should use a background thread with `Task.Factory.StartNew` take a look at https://stackoverflow.com/questions/26921191/how-to-pass-longrunning-flag-specifically-to-task-run – TheGeneral Sep 14 '20 at 04:43
  • @Michael Randall: So that means without specifying LongRunning, it will not be able to handle such a large running background process? – user3458219 Sep 14 '20 at 05:12
  • `Task.Run` will run fine and has certain benefits, and will achieve everything you want to acheive. If you have a problem (which id doubt), you can look at `Task.Factory.StartNew` – TheGeneral Sep 14 '20 at 05:15
  • _"what's best"_ questions are primarily opinion based and unsuitable for Stack Overflow. _"any better way"_ questions are too broad. If you want to understand whether `Task.Run()` is more suitable for your scenario than `StartNew()`, see duplicate. Please read [ask] and other on-site resources so that you understand better what makes a suitable, well-written question for Stack Overflow. – Peter Duniho Sep 14 '20 at 07:22
  • In the vast majority of situations, the thread pool will adjust to any long-running task in 0.5 seconds - without the LongRunning flag. Most likely, you don’t really need it. https://blog.stephencleary.com/2013/08/startnew-is-dangerous.html – user3458219 Sep 14 '20 at 08:27

0 Answers0