Quantcast
Viewing all articles
Browse latest Browse all 12583

Background worker thread not closing

Greetings

I'm making a program that moves some files around using a backgroundWorker and the problem that I'm facing is that I cannot seem to be able to close it down on the frame_close event.

I'm using this:

private void movingFiles_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
           // ssh.Get(

            int i = 1 + 1;
            this.Invoke(new Action(delegate()
                    {
            log(i.ToString());
                    }));
        }  


Log simply writes in a multiline textblock and a file with "using"

using (StreamWriter w = File.AppendText("E:\\Capone Reports Mover v. 1.0 Log.txt"))
                    {
                        w.Write(DateTime.Now + "    " + message + Environment.NewLine);
                    }


Found this bit while searching for more information regarding my problem.

 protected override void OnClosed(EventArgs e)
        {

            if (movingFiles_backgroundWorker.CancellationPending)
            {
                log("cancelation pending");
            }
            while (movingFiles_backgroundWorker.IsBusy)
            {
                Thread.Sleep(500);
                log("is busy");
            }
            base.OnClosed(e);
        }

And also 3 buttons, one that tests, one that disposes and one that does CancelAsync()

private void checkBackgroundWorker_button_Click(object sender, EventArgs e)
        {
            if (movingFiles_backgroundWorker.IsBusy)
            {
                log("backgrounWorker is busy");
            }
            else
            {
                log("backgroundWorker is not busy");
            }
        }

This always returns "backgroundWorker is not busy"


private void backgroundWorkerDispose_button_Click(object sender, EventArgs e)
        {
            try
            {
                movingFiles_backgroundWorker.Dispose();
                log("disposed");
            }
            catch
            {
                log("couldn't dispose");
            }
        }

This returns "disposed" all the time

private void backgroundWorkerCancel_button_Click(object sender, EventArgs e)
        {
            try
            {
                movingFiles_backgroundWorker.CancelAsync();
                log("canceled");
            }
            catch
            {
                log("couldn't cancel");
            }
        }

And this last one always "canceled"

So the form closes but VS is still in debug mode because of the thread hanging around. And if you're wondering, yes I did enabled "WorkerSupportsCancellation"

Looking forward to suggestions

Thank you for your time :)




Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>