I need to develop a C# application using **BackgrounWorker**
, but without using Windows Forms. I've seen some examples presented by Microsoft regarding the use of the BackgrounWorker class, and they're all developed specifically for Windows Forms. Can I use BackgroundWorker without Windows Forms ? if so, please provide some detail. thanks in advance !
Asked
Active
Viewed 2,494 times
1

ekremer
- 311
- 4
- 23
-
3The BackgroundWorker was designed for use with Windows Forms. The ProgressChanged() and RunWorkerCompleted() events are automatically marshaled to the main UI thread so it is safe to interact with the UI. If you have no UI then why use the BackgroundWorker?...where does the "need" to do this come from? There are other options, such as manually creating [Threads](https://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.110).aspx) or using [Tasks](https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx). – Idle_Mind Jul 17 '15 at 02:16
-
Thanks for your prompt and insightful response !!! The "need" comes from the fact that I inherited a **Windows Forms** application that uses **BackgroundWorker**, and I needed it to connect with another application in **asp.net Web-API**. However, these two frameworks cannot talk directly. So, I am trying to extract the code from the Windows Forms application to put it into a **"pure" c#** application. What is your suggestion between the 2 options: Threads and Tasks ? – ekremer Jul 17 '15 at 02:32
-
Thread is the old school way of doing it. If you have no compelling reason to be "old school", then definitely learn how to use Tasks. See [SLaks Blog](http://blog.slaks.net/2013-10-11/threads-vs-tasks/) for some more insight on Task vs. ThreadPool, however. – Idle_Mind Jul 17 '15 at 02:47
-
Thanks for your explanation and the blog ! – ekremer Jul 17 '15 at 03:08
1 Answers
2
In C# there you can use Tasks or Threads they are both used to run in background in separated execution context ( separated thread )
This is an simple example for using thread
using System.Threading;
// creating new thread and pass it the delegate to run it
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
// this command to start the thread
thread.Start();
public void WorkThreadFunction()
{
// do any background work
}

Bakri Bitar
- 1,543
- 18
- 29
-
**Hi Bakri**, thanks for your comments and the example ! I think I will use **Threads**. – ekremer Jul 17 '15 at 02:54