1

I have two scenarios and I am not sure whether to use Tasks or Threads (working with dot net Framework 4.7)

Scenario 1: Database Queries

I have a set of data base queries that I execute with SqlCommand class. I need all of the given queries to run in parallel. Queries run from 1 min to 60 minutes.

What is better in this situation? Is the Thread/Task actually "active" when it is just waiting for the query to be completed?

Scenario 2: Parsing XML

I have let's say 1000 XmlFile Objects in a List or Array. I have a set of XPath Expressions that need to be parsed from those 1000 files into a DataTable object ( for later deployment to a database ). The best scenario would be that as many of those 1000 Files get parsed in parallel.

Parsing should take not too long but definitely longer than a few seconds. I implemented this currently with Task.Run() and it takes around 90 Seconds for 1000 although i cannot really monitor how much of it is running parallel.

Are there also general rules on when to take what?

Thanks

tuxmania
  • 906
  • 2
  • 9
  • 28
  • First, in general use Tasks over Threads. Second, if you want to run stuff in parallel consider using the Parallel.For/ForEach methods which will provide automatic load-balancing and other things for optimal usage of your CPU to improve processing times and optimal resource usage. – ckuri Jan 30 '19 at 20:06
  • In your first scenario which is pure I/O bound work use the Async methods of SqlCommand object which will provide asynchronous processing without needing any threads. Therefore there is no need for additional threads or Task.Run – rather either would likely decrease your performance. – ckuri Jan 30 '19 at 20:12
  • In your second example don’t use Task.Run but instead Parallel.For/ForEach which provides a much more optimal resource handling if you want to run lots of work items in parallel like your 1000 file parse operations. – ckuri Jan 30 '19 at 20:19

1 Answers1

1

The Thread is used for creating and manipulating a thread in Windows.

A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.

Consider that Task is more abstract then threads, it is generally created on the thread pool which are treated as background threads while thread is by default not background which can verified by the code present in two below figures.

In this way, I would recommend to use the higher level abstraction possible. So you should use tasks instead of thread as it is created on the thread pool which has already system created threads to improve the performance and you don\t need to worry about explicitly start/manage your own thread.

LA.

leandro.andrioli
  • 997
  • 5
  • 12
  • *generally created on the thread pool* not really. Consider a `async` operation – Liam Jan 30 '19 at 16:34
  • *Consider that Task is more abstract then threads* is spot on, but a lot of this is a bit shaky – Liam Jan 30 '19 at 16:34