Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another Background worker

I have looked at msdn and similar questions of stack exchange for how to use background workers.

Basically, my function upload-program does the actual work, but I want a thread to change elements of the ui as it goes (progress bars etc) and as i send events to progress changed. What I've tried is below (harshly edited), it doesn't work and the program seems to cut out after the call to runworkerasync. Is there something simple wrong or was it wrong to send my command "to the other thread"?

BackgroundWorker backgroundUpload = new System.ComponentModel.BackgroundWorker();

The first bit is the call:

        if (backgroundUpload.IsBusy != true)
        {
            backgroundUpload.RunWorkerAsync(work);
                 // a command here for debug purposes (eg a message box) will run
        }
        else
        { //it doesn't go here, this isn't the error}

Then the dowork, it never seems to get here.

    private void backgroundUpload_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        e.Result = UploadProgram((Workload)e.Argument, worker, e); //workload is one of my enums
    }

never seems to get here either.

bool UploadProgram(Workload work, BackgroundWorker worker, DoWorkEventArgs e)
    {
    }

    //also there is progress changed and run worker complete.
like image 419
Nathan Cooper Avatar asked Apr 25 '26 12:04

Nathan Cooper


2 Answers

Do you subscribe your event handler to the event?

BackgroundWorker backgroundUpload = new System.ComponentModel.BackgroundWorker();
backgroundUpload.DoWork += backgroundUpload_DoWork;
like image 66
Douglas Avatar answered Apr 28 '26 02:04

Douglas


Silly question, but are you sure you subscribed to the event?

backgroundUpload.DoWork += backgroundUpload_DoWork;

Here is a complete example, downloading a file

like image 32
Davio Avatar answered Apr 28 '26 01:04

Davio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!