backgroundworker RunWorkerCompleted没有解雇"有时"

时间:2014-03-17 17:42:54

标签: c# backgroundworker filesystemwatcher

我有一个filesystemwatcher,可以在发生更改时检查文件。这连续运行4次,但在第5次尝试时,formwaitingform没有关闭,并且没有调用backgroundWorker1_RunWorkerCompleted。我尝试删除打开和关闭繁忙表单的代码,然后代码按预期运行,所以看起来这是问题。我的错误陷阱中没有错误但是怀疑问题与filesystwatcher在另一个线程中触发相关而无法关闭繁忙的表格,但我不确定。任何帮助表示赞赏。

    public partial class Main : Telerik.WinControls.UI.RadForm, IMessageFilter
    {

        WaitingForm formWaitingForm;
        BackgroundWorker bw = new BackgroundWorker(); // Backgroundworker
        public Main()
        {
......

public void OnChanged(object sender, FileSystemEventArgs e)
        {
            try
            {
                // Do not add logging before first line as we want to set the event change immediately to endure multiple events are not fired for 
                // multiple changes simultaneously


                if (MyGlobals.LastFired.Add(TimeSpan.FromSeconds(1)) < DateTime.UtcNow) //http://stackoverflow.com/questions/22236363/filesystemwatcher-for-a-list-of-files/22236688#22236688
                {
                    MyGlobals.LastFired = DateTime.UtcNow;
                    Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1); // See comment inside try
                    System.Threading.Thread.Sleep(2000);
                    Logging.Write_To_Log_File("Item change detected " + e.ChangeType + " " + e.FullPath + " " + e.Name, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);


                    MyGlobals.watchers.Clear();
                    foreach (FileSystemWatcher element in MyGlobals.watchers)
                    {
                        element.EnableRaisingEvents = false;
                    }
                    CheckFilesAsync(); // Get the timer to execute immediately

                }
                else
                {
                    Logging.Write_To_Log_File("Change within the time limit", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
                }




                //Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
                return;

            }


            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), "", "", "", 2);
            }

            finally
            {
                foreach (FileSystemWatcher element in MyGlobals.watchers)
                {
                    element.EnableRaisingEvents = true;
                }
            }


        }


 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
                lock (MyGlobals._object) // Lock for threadsafe operations
                {

                    // First, handle the case where an exception was thrown. 
                    if (e.Error != null)
                    {
                        RadMessageBox.Show(this, e.Error.Message, "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                        Logging.Write_To_Log_File(e.Error.Message, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);

                    }
                    else if (e.Cancelled)
                    {
                        // Next, handle the case where the user canceled the operation. 
                        RadMessageBox.Show(this, "Background task cancelled", "Cancel", MessageBoxButtons.OK, RadMessageIcon.Error);
                        Logging.Write_To_Log_File("Task cancelled", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
                    }
                    else
                    {
                        // Finally, handle the case where the operation succeeded.

                        Logging.Write_To_Log_File("Finished background worker - closing Form", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);

                        this.BeginInvoke((MethodInvoker)(() => formWaitingForm.Close()));
                        this.BeginInvoke((MethodInvoker)(() => formWaitingForm.Dispose()));
                        //formWaitingForm.Close();
                        //formWaitingForm.Dispose();
                        Logging.Write_To_Log_File("Form closed", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);

                    }
                   // _timer.Enabled = true;
                }
                Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
            }
            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), "", "", "", 2);
            }



        }

1 个答案:

答案 0 :(得分:0)

Filesystemwatchers在文件更改时引发事件 - 我想触发CheckFilesAsync,然后打开一个繁忙的表单,最后关闭此表单。在主线程中调用Checkfilesasync,如下所示解决了我的问题。

   this.BeginInvoke((MethodInvoker)(() => CheckFilesAsync())); // Check files in the Main thread otherwise threading issues occur