-1

I have a program handling automation of machines. One of the functions is to wait for a file to be produced and then do something with it. My problem is that if someone cancels the external process, the file will never appear and my program gets stuck waiting for the file.

I have a 'Cancel' button on my form which will cancel the backgroundWorker doing the work - the cancellation is checked at the beginning of a for loop which is the main crux of the backgroundWorker. The FileSystemWatcher method is called from a backgroundWorker thread, and seemingly cannot be interrupted like normal threads.

private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
for (int i = 0; i<listBox1.Items.Count;i++)
{

    //check for stop

    if (backgroundWorker1.CancellationPending)
        break;



    switch (LineItm[0])
    {
        case "OUTPUT":
            {
                // do stuff
            }
        case "LOOP":
            {
                //do stuff
            }
        case "LOOP UNTIL":
            {
                \\ do stuff
            }
        case "WAIT":
            {

                switch ()
                {
                    case "INPUT":

                        // do stuff

                    case "TIME":
                        //do stuff
                    case "RESULT":
                        //find result file

                        string csv = WaitforFile(ResultPath);
                        if (csv == "")
                        {
                            MessageBox.Show("Result timeout!", "Timeout", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        Result_File = csv;
                        Thread.Sleep(100);
                        break;

                }
                break;
            }
        case "FUNCTION":
            {
                // do stuff
            }

    }

}

private string WaitforFile(string dir)
        {
            using (var watcher = new FileSystemWatcher(dir, "*.csv"))
            {
                watcher.EnableRaisingEvents = true;

                var watcherCreatedFile = watcher.WaitForChanged(WatcherChangeTypes.Created);
                //this will wait for a file to be created

                if (watcherCreatedFile.ChangeType == WatcherChangeTypes.Created)
                {
                    //will trigger when a file is created
                    string fileNameCreated = watcherCreatedFile.Name;
                    return fileNameCreated;
                }
            }
            return "";
        }

At the moment, if i click "Cancel" on the form during a WaitForChanged event, it gets stuck.

How can interrupt this process?

MJ2507
  • 113
  • 1
  • 12

1 Answers1

1

You could create a class that inherits from BackgroundWorker. The class would contain a bool for "Signal Shutdown", in which case you could just check for that bool and terminate the worker. Maybe someone else will have a better answer in regards to managing the cancellation token.

Edit - Looking at a few threads, make sure you init your BackgroundWorker like so

    var worker = new BackgroundWorker { WorkerSupportsCancellation = true };

Cancelling a BackgroundWorker

Zakk Diaz
  • 1,063
  • 10
  • 15