Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 后台工作人员Runworker完成未解雇“;有时";_C#_Backgroundworker_Filesystemwatcher - Fatal编程技术网

C# 后台工作人员Runworker完成未解雇“;有时";

C# 后台工作人员Runworker完成未解雇“;有时";,c#,backgroundworker,filesystemwatcher,C#,Backgroundworker,Filesystemwatcher,我有一个filesystemwatcher,在发生更改时检查文件。这将连续运行4次,但在第5次尝试时,formwaitingform未关闭,backgroundWorker1_RunWorkerCompleted未调用。我尝试删除打开和关闭繁忙窗体的代码,然后代码按预期运行,因此似乎这就是问题所在。我的错误捕获中没有出现错误,但怀疑问题与FileSystemWatcher在不同线程中触发有关,并且没有权限关闭繁忙窗体,但我不确定。谢谢你的帮助 public partial class M

我有一个filesystemwatcher,在发生更改时检查文件。这将连续运行4次,但在第5次尝试时,formwaitingform未关闭,backgroundWorker1_RunWorkerCompleted未调用。我尝试删除打开和关闭繁忙窗体的代码,然后代码按预期运行,因此似乎这就是问题所在。我的错误捕获中没有出现错误,但怀疑问题与FileSystemWatcher在不同线程中触发有关,并且没有权限关闭繁忙窗体,但我不确定。谢谢你的帮助

    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);
            }



        }
public部分类Main:Telerik.WinControls.UI.RadForm、IMessageFilter
{
waitingformwaitingform;
BackgroundWorker bw=新的BackgroundWorker();//BackgroundWorker
公用干管()
{
......
更改后的公共void(对象发送方、文件系统目标)
{
尝试
{
//不要在第一行之前添加日志记录,因为我们希望立即设置事件更改以承受多个事件不会触发
//同时进行多个更改
if(MyGlobals.lastfield.Add(TimeSpan.FromSeconds(1))formWaitingForm.Close());
this.BeginInvoke((MethodInvoker)(()=>formWaitingForm.Dispose());
//formWaitingForm.Close();
//formWaitingForm.Dispose();
Logging.Write到日志文件(“Form closed”,MethodBase.GetCurrentMethod().Name,”,“”,“”,“”,“”,“”,1);
}
//_timer.Enabled=真;
}
日志记录。将写入日志文件(“Exit”,MethodBase.GetCurrentMethod().Name,”,“,”,“,”,“,”,1);
}
捕获(例外情况除外)
{
//如果发生异常,将在此处返回
Logging.Write到日志文件(“Error”,MethodBase.GetCurrentMethod().Name,”,“”,例如ToString(),“”,“”,“”,“”,2);
}
}

文件系统监视程序在文件更改时引发事件-我想触发CheckFileAsync,它会打开一个繁忙的表单,最后关闭此表单。在主线程中调用CheckFileAsync(如下所示)可以解决我的问题

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

可能与@GrantWinney更新的问题有关clarify@GrantWinney在dowork事件中,如果我使用此链接,会不会使线程问题消失?@GrantWinney嘿,请看下面我的答案-到目前为止,这似乎工作正常。