线程中断后无法删除文件

时间:2019-03-07 00:23:52

标签: c# multithreading

我有点迷失了这个;我已经尝试过执行此操作所知道的一切,并且错误仍然存​​在。

我有一个FileProcessor类,它创建一个新线程,执行一些操作等;但是,即使手动调用其中的Dispose()和Thread.Interrupt(),使用后我似乎也无法删除文件。

首先,我在主线程上使用async方法执行此代码;现在我已切换到使用此FileProcessor进行线程化,只是尝试在操作后删除那些文件。

我可以删除一个或两个文件,但是当它到达第三个文件时,它将引发System.IOEXception

我真的不知道还能做什么。任何输入表示赞赏。 我在Dispose()中使用Worker.Join并等待线程完成或GC结束它-但都没有发生。

谢谢

我的代码(尽可能减少) 表格1:

using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private bool RestartTimer;
        private bool ThreadRunning;
        FileProcessor TIFFtoXMLProcessor;
        FileProcessor CIP3toTIFFProcessor;
        List<string> files;
        public Form1()
        {
            InitializeComponent();
            TIFFtoXMLProcessor = new FileProcessor();
            RestartTimer = false;
        }
        private void BeginWork()
        {
            TIFFtoXMLProcessor.EnqueueFileName(@"C:\test\yourtestfile1.txt");
            TIFFtoXMLProcessor.EnqueueFileName(@"C:\test\yourtestfile2.txt");
            TIFFtoXMLProcessor.EnqueueFileName(@"C:\test\yourtestfile3.txt");
            files = new List<string>(TIFFtoXMLProcessor.fileNamesQueue);
            TIFFtoXMLProcessor.eventWaitHandle.Set();
            if(TIFFtoXMLProcessor.worker.IsAlive == false)
            {
                foreach(var item in files)
                {
                    System.IO.File.Delete(item);
                }
            }
        }
    }
}

FileProcessor类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace WindowsFormsApp1
{
    class FileProcessor : IDisposable
    {
        public EventWaitHandle eventWaitHandle { get; private set; }
        public Thread worker { get; private set; }
        private readonly object locker = new object();
        public Queue<string> fileNamesQueue { get; private set; }
        public string currConversion { get; private set; }
        public bool JobComplete { get; private set; }        
        private CancellationTokenSource cancelParallelWorker;
        public string ColorSeparator { get; private set; }        
        private readonly TextBox tbStatus;
        public string outputFolder { get; private set; }
        List<string> filesgoingtorun;

        //var AvailableJobsDictionary = new Dictionary<string, List<string>>();
        //string nZones, string zWidth, string fzWidth, string lzWidth, string zAreaWidth, string zAreaHeight, double DPI
        public FileProcessor()
        {

            eventWaitHandle = new AutoResetEvent(false);
            fileNamesQueue = new Queue<string>();
            // Create worker thread
            worker = new Thread(Work)
            {
                IsBackground = true
            };
            cancelParallelWorker = new CancellationTokenSource();
            worker.Start();
        }
        public void EnqueueFileName(string FileName)
        {
            // Enqueue the file name
            // This statement is secured by lock to prevent other thread to mess with queue while enqueuing file name
            lock (locker) fileNamesQueue.Enqueue(FileName);
            // Signal worker that file name is enqueued and that it can be processed
            //eventWaitHandle.Set();
        }
        private void Work()
        {
            List<string> filesToWork = new List<string>();
            while (true)
            {
                string fileName = null;
                // Dequeue the file name
                lock (locker)
                    while (fileNamesQueue.Count > 0)
                    {
                        fileName = fileNamesQueue.Dequeue();
                        filesToWork.Add(fileName);
                        if (fileName == null) return;
                    }
                if (fileNamesQueue.Count == 0 && filesToWork.Count > 0)
                {
                    var tempList = new List<string>(filesToWork);
                    filesToWork.Clear();
                    ProcessJob(tempList);
                }

            }
        }
        private void ProcessJob(List<string> filesToWork)
        {
            try
            {
                JobComplete = true;
                switch (currConversion)
                {
                    case "TIF":
                        {
                            int j = 0;
                            foreach (var currJob in filesToWork)
                            {
                                //Series of tasks...
                                j++;
                            }
                            eventWaitHandle.WaitOne();
                            break;
                        }
                }
                JobComplete = false;
                Dispose();
            }
            catch (Exception conversionEx)
            {
                cancelParallelWorker?.Cancel();                
            }
        }   
        #region IDisposable Members
        public void Dispose()
        {
            // Signal the FileProcessor to exit
            EnqueueFileName(null);
            // Wait for the FileProcessor's thread to finish
            worker.Interrupt();
            // Release any OS resources
            eventWaitHandle.Close();
        }

        #endregion
    }

}

1 个答案:

答案 0 :(得分:1)

您的代码对于要执行的操作异常复杂,也就不足为奇了,您在其他线程上打开了文件句柄的某个位置,这使您的代码无法删除该文件。在此之前无法重现该问题,我什至可以开始弄清楚您应该怎么做。

但这是我要建议的方法。

您应该使用Microsoft的Reactive Framework(又名Rx)-NuGet System.Reactive.Windows.Forms并添加using System.Reactive.Linq;-然后您可以执行以下操作:

public partial class Form1 : Form
{
    private Subject<string> _enqueue = new Subject<string>();
    private IDisposable _subscription = null;

    public Form1()
    {
        InitializeComponent();

        string ColorSeparator = "42";
        int imageRotationNumber = 42;

        IObservable<string> query =
            from file in _enqueue
            from ImageListSorted in Observable.Start(() => ImageBuilder(file, ColorSeparator))
            from RotateCMYK in Observable.Start(() => Rotate(ImageListSorted.CMYKmages, imageRotationNumber))
            select file;

        _subscription = query.Subscribe(f => System.IO.File.Delete(f));

        _enqueue.OnNext(@"C:\test\yourtestfile1.txt");
        _enqueue.OnNext(@"C:\test\yourtestfile2.txt");
        _enqueue.OnNext(@"C:\test\yourtestfile3.txt");
    }


    private CreateCMYKAndImpositionImageList ImageBuilder(string JobImages, string colorDelimiter)
    {
        return new CreateCMYKAndImpositionImageList(JobImages, colorDelimiter);
    }

    private RotateImages Rotate(Dictionary<string, string> imageList, int RotationNumber)
    {
        return new RotateImages(imageList, RotationNumber);
    }
}

现在,我仅在您的流程中包括两个步骤,但是您应该能够继续执行其余步骤的逻辑。

每个步骤都是异步运行的,可以通过调用_subscription.Dispose();随时取消整个操作。

所有步骤完成后,才能打最后的.Subscribe(f => System.IO.File.Delete(f))

只要您避免与线程和任务相关的任何事情,它就应该运行得很干净。