Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming BackgroundWorker

Tags:

c#

wpf

I would like to be able to name a BackgroundWorker to make it easier to debug. Is this possible?

like image 691
Robert Avatar asked Jul 16 '10 17:07

Robert


People also ask

What is a BackgroundWorker?

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running.

What is the difference between BackgroundWorker and thread?

A BackgroundWorker is a ready to use class in WinForms allowing you to execute tasks on background threads which avoids freezing the UI and in addition to this allows you to easily marshal the execution of the success callback on the main thread which gives you the possibility to update the user interface with the ...

What is a BackgroundWorker in C#?

A BackgroundWorker component executes code in a separate dedicated secondary thread. In this article, I will demonstrate how to use the BackgroundWorker component to execute a time-consuming process while the main thread is still available to the user interface.

How do I get rid of BackgroundWorker?

BackgroundWorker has its own, unique way of doing cancellation. First, when constructing the BGW instance, be sure to set BackgroundWorker. WorkerSupportsCancellation to true . Then, the calling code can request the worker to cancel by calling BackgroundWorker.


3 Answers

I'd have to try but can't you just set the Name of the thread in the DoWork() method executed by the BackgroundWorker?

UPDATE: I just tried the following line of code as the first statement of my BackgroundWorkers DoWork() method and it works:

if (Thread.CurrentThread.Name == null)
    Thread.CurrentThread.Name = "MyBackgroundWorkerThread";

UPDATE: As Jonathan Allen correctly stated the name of a thread is write once, so I added a null check before setting the name. An attempt to write the name for the second time would result in an InvalidOperationException. As Marc Gravell wrote it might also make debugging harder as soon as pooled background threads are re-used for other work, so name threads only if necessary..

like image 81
andyp Avatar answered Oct 09 '22 05:10

andyp


public class NamedBackgroundWorker : BackgroundWorker
{
    public NamedBackgroundWorker(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }

    protected override void OnDoWork(DoWorkEventArgs e)
    {
        if (Thread.CurrentThread.Name == null) // Can only set it once
            Thread.CurrentThread.Name = Name;

        base.OnDoWork(e);
    }
}
like image 38
XIU Avatar answered Oct 09 '22 03:10

XIU


You can extend the background worker through a custom class:

`

public class NamedBackgroundWorker : BackgroundWorker
{

     public string Name;

     public BackgroundWorker(string Name)
     {
       this.Name = Name;
     }
}

` Now just create an object from this and you can name it and use it as a background worker.

like image 2
Aleksandar Rajkovic Avatar answered Oct 09 '22 04:10

Aleksandar Rajkovic