FileProcessor线程未结束

帕布洛

成员
已加入
2018年9月4日
留言内容
6
编程经验
1-3
大家好。
我在FileSystemWatcher中使用FileProcessor类(网络上有很多示例)来处理FileSystemWatcher排队的福彩12选5走势图。

我修改了此福彩12选5走势图以满足我的需要,但是我缺少了一些东西-当它完成对作业的处理并返回到UI线程时,我的FileSystemWatcher停止监视福彩12选5走势图。
我以我制作的其他程序为例,无需重新启动FileSystemWatcher;它一直在运行,监视创建的福彩12选5走势图。

这是我正在尝试尝试查找问题的调试方式:
1-程序启动
2-Timer和FileSystemWatcher正在运行
2.1-如果未创建福彩12选5走势图,计时器将重新启动。如果创建了福彩12选5走势图,计时器将重新启动。
2.2-如果计时器结束并且队列中有福彩12选5走势图,请处理与这些福彩12选5走势图有关的作业。
3-处理后,结束FileProcessor线程并返回到UI
4-计时器重新启动(处理福彩12选5走势图后,我确实在UI线程上重新启动了计时器),但是FileSystemWatcher停止工作。

我也应该重新启动FileSystemWatcher吗?我对此表示怀疑,因为如果这样做,我可能会在进程运行时丢失一些福彩12选5走势图事件(这将是一个漫长的过程)。

任何帮助表示赞赏。谢谢 !

我将在此处发布我的代码。
FileProcessor.cs
C#:
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Timers;
using DI_Plot_2018.IZData;


namespace DI_Plot_2018
{
    class FileProcessor : IDisposable
    {
        // Create an AutoResetEvent EventWaitHandle
        private EventWaitHandle eventWaitHandle = new AutoResetEvent(false);
        private readonly object locker = new object();
        private Thread worker;                
        public Queue<string> FilesQueue = new Queue<string>();
        private Queue<KeyValuePair<string, List<string>>> JobsQueue = new Queue<KeyValuePair<string, List<string>>>();
        private Dictionary<string, List<string>> FullJob = new Dictionary<string, List<string>>();
        #region IDisposable Members
        public FileProcessor()
        {
            worker = new Thread(new ThreadStart(Work));
            worker.IsBackground = true;
        }
        public void FireProcessing(double counter)
        {
            if (counter <= 0) //Timer is over, proceed with execution
            {
                lock(locker)
                {
                    while (FilesQueue.Count > 0)
                    {
                        string currFilename = FilesQueue.Dequeue(); //Dequeue filename
                        JobDetails CurrentJobInfo = new JobDetails(currFilename); //Get information about the job 
                        //Checking if dict already has the key - if it does, add the filename at the key position
                        if (FullJob.ContainsKey(CurrentJobInfo.JobNameWithoutColor))
                            FullJob[CurrentJobInfo.JobNameWithoutColor].Add(currFilename);
                        else //if it doesn't, add the new key and start the value (List<string>) with the current filename
                            FullJob.Add(CurrentJobInfo.JobNameWithoutColor, new List<string>() { currFilename });
                    }//End while loop - Dequeue all files
                    foreach (var item in FullJob) //Enqueue files in a jobs queue (KeyValuePair queue)
                    {
                        JobsQueue.Enqueue(item);
                    }
                    eventWaitHandle.Set();                    
                    worker.Start();
                }                                    
                
            }
        }
        public void EnqueueJob(string FileName, double counter)
        {            
            if(counter > 0)
            {
                lock(locker)
                {
                    FilesQueue.Enqueue(FileName);
                }                
            }             
        }
        private void Work()
        {
            while(true)
            {
                KeyValuePair<string, List<string>> JobToRun = new KeyValuePair<string, List<string>>();
                lock (locker)
                {
                    if(JobsQueue.Count > 0)
                    {
                        JobToRun = JobsQueue.Dequeue();
                        if (JobToRun.Key == null) return;
                    }
                    if(JobToRun.Key != null)
                    {
                        ProcessJob(JobToRun);
                    }
                    else
                    {
                        eventWaitHandle.WaitOne();
                    }
                }
            }
        }
        private void ProcessJob(KeyValuePair<string,List<string>> currJob)
        {
            string x = string.Empty;
            
        }
        public void Dispose()
        {
            // Signal the FileProcessor to exit
            //FilesQueue.Enqueue(null);
            JobsQueue.Enqueue(new KeyValuePair<string, List<string>>("aa", new List<string>() { "aa"}));
            // Wait for the FileProcessor's thread to finish
            worker.Join();
            // Release any OS resources
            eventWaitHandle.Close();
        }
        #endregion
    }
}

Form1-启动计时器并启用EnableRaisingEvents属性
C#:
 private void ButtonChangedEventHandler(object sender, EventArgs e)
        {
            Telerik.WinControls.UI.RadButton bt = sender as Telerik.WinControls.UI.RadButton;
            string buttonName = (String)bt.Name;
            SwitchButtonImage buttonImageSwitch;
            switch (buttonName)
            {
                case "StartButton":
                    {
                        InputFileWatcher = new FileSystemWatcher(InputFolderTextBox.Text);
                        InputFileWatcher.Created += new FileSystemEventHandler(FileCreated);
                        InputFileWatcher.Filter = "*.tif";
                        InputFileWatcher.EnableRaisingEvents = true;
                        StopButton.Enabled = true;
                        StartButton.Enabled = false;
                        CountDown(); //Restarts the timer
                        break;
                    }
       }


计时器滴答-如果需要,我可以在其中重新启动计时器或处理队列中的内容
C#:
private void SecondElapsed(object sender, EventArgs e)
        {
            double remainingSeconds = s - (DateTime.Now - start).TotalSeconds;            
            if(RestartTimer == true)
            {
                timer1.Stop();
                CountDown();
                RestartTimer = false;
            }
            else if (remainingSeconds <= 0)
            {
                timer1.Stop();
                if(TIFFProcessor.FilesQueue.Count > 0)
                {
                    TIFFProcessor.FireProcessing(CurrentCounter);
                }                
                CountDown();
            }

我知道自昨天以来我一直在发布一些新手问题-除了阅读有关MSDN的几篇文章外,我正在尽力向帖子/其他用户学习。

再次感谢您!
 
最佳 底部