Velocedge
Velocedge

Reputation: 1455

C# Use async or thread for long running database activity in Azure SQL

I have a nightly C# Windows service that updates tables and calls a long running (~ hour) stored procedure on 5-10 Azure SQL databases. Since I can run all database work at the same time in Azure SQL with little to no performance hit, I'd like to run them asynchronously. I can either call a method with async void (using Await Task.Delay(10)) or create a new thread.

What is the better approach?... does it matter?

EDIT: Here is an example of the asynch void method. I want to make sure it's clear what it's doing.

 public static async void DoRanking(object e)
    {
        // will start all orgs concurrently
        await Task.Delay(10);
        ... do a bunch of database work here ...
        ... call a long running stored procedure here ...
    }

Upvotes: 0

Views: 558

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89361

You can use the older Thread/ThreadPool APIs or the newer TPL. But in either case you shouldn't just start a long-running process on a threadpool thread.

Use a new Thread or a LongRunning Task:

Specifies that a task will be a long-running, coarse-grained operation involving fewer, larger components than fine-grained systems. It provides a hint to the TaskScheduler that oversubscription may be warranted. Oversubscription lets you create more threads than the available number of hardware threads. It also provides a hint to the task scheduler that an additional thread might be required for the task so that it does not block the forward progress of other threads or work items on the local thread-pool queue.

Documentation for TaskCreationOptions.LongRunning

Upvotes: 2

Related Questions