简体   繁体   中英

On what thread does this method runs? C#

I have a method that updates records from the database, and I wonder if this method really runs in my BackGroundWorker thread considering the following:

public partial class Form1 : Form
{
    BackgroundWorker bg = new BackgroundWorker();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       bg.DoWork += new DoWorkEventHandler(bg_DoWork);
       bg.RunWorkerAsync();
    }

    void bg_DoWork(object sender, DoWorkEventArgs e)
    {
        UpdateDatabaseRecords(); // <-- Does this method runs in my BackGroundWorker?
    }

    private void UpdateDatabaseRecords()
    {
        SqlConnection conn = new SqlConnection();
        // etc...
    }
}

Is there a difference if I coded the update stuffs directly inside the bg_DoWork method?
Something like:

void bg_DoWork(object sender, DoWorkEventArgs e)
{
    SqlConnection conn = new SqlConnection();
    // etc...
    // do the update codes here instead of doing 
    // it by calling another method.
}

Yes it is executing on a separate thread. No there wouldn't be a difference thread wise if you put it directly in that method.

Functions run in the thread that calls them, due to how function calls are implemented. So, since your background worker is calling the bg_DoWork function, it will be running in the worker's thread.

Because the code snippet appears small, there probably won't be a significant difference in calling another function. If you're just doing that little bit of work, then you can have it all in one function. If you start to increase the complexity of what the worker does, then you may want to start splitting it into many functions.

No, there is no difference. Invoking a method creates a new stack-frame for the method call, pushes it onto the call-stack for the current thread , and then transfers control to it. It's also possible that the method may be inlined by the JIT compiler, so you may not see any difference in the disassembly between your 'manually inlined' version and your current version.

Btw, here's the code for BackgroundWorker.RunAsync from reflector:

public void RunWorkerAsync()
{
    this.RunWorkerAsync(null);
}

public void RunWorkerAsync(object argument)
{
    if (this.isRunning)
    {
        throw new InvalidOperationException(SR.GetString("BackgroundWorker_WorkerAlreadyRunning"));
    }
    this.isRunning = true;
    this.cancellationPending = false;
    this.asyncOperation = AsyncOperationManager.CreateOperation(null);

    // the important bit
    this.threadStart.BeginInvoke(argument, null, null);
}

As you can see, your code will run in the context of a WorkerThreadStartDelegate.BeginInvoke . This should mean that one of the thread-pool threads will pick it up, which you can verify by testing the value of Thread.CurrentThread.IsThreadPoolThread inside the bg_DoWork method.

Yes it runs in a separate thread (background). The only difference is that you don't have access to the DoWorkEventArgs parameter, but you can pass it to your method.

I don't think so!

wrapping it in a method don't make it work in different thread, i think all of your code inside bg_DoWork will work on background worker (including all code on UpdateDatabaseRecords method).

there is a ThreadSynchronizationContext class where you can post your method to work on different thread context.

you can test your code on visual studio by put a break point inside bg_DoWork method and UpdateDatabaseRecords method. check it out from "Thread Window" from menu "Debug -> Windows-> Thread" investigate it weather it is work on main thread or worker thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM