C# Can';线程中断后,不能删除文件

C# Can';线程中断后,不能删除文件,c#,multithreading,C#,Multithreading,我有点迷路了;我已经尝试了我所知道的一切来做这样的操作,错误仍然存在 我有一个FileProcessor类,它创建一个新线程,执行一些操作,等等;但是,即使在它内部手动调用Dispose()和Thread.Interrupt()时,我似乎也无法在使用后删除这些文件 首先,我在主线程上使用异步方法编写代码;现在,我已经切换到使用此文件处理器执行线程,只是尝试在操作后删除这些文件 我可以删除一个或两个文件,但当它到达第三个文件时,会抛出System.IOEXception 我真的不知道我还能做什么。

我有点迷路了;我已经尝试了我所知道的一切来做这样的操作,错误仍然存在

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

首先,我在主线程上使用异步方法编写代码;现在,我已经切换到使用此文件处理器执行线程,只是尝试在操作后删除这些文件

我可以删除一个或两个文件,但当它到达第三个文件时,会抛出System.IOEXception

我真的不知道我还能做什么。欢迎您的任何意见。 我正在使用Worker.Join-inside Dispose()并等待线程完成或GC结束它,但这两种情况都没有发生

谢谢

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

使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
公共部分类Form1:Form
{
私人bool重启计时器;
私家车;
文件处理器;文件处理器;
文件处理器CIP3toTIFFProcessor;
列出文件;
公共表格1()
{
初始化组件();
TIFFtoXMLProcessor=新文件处理器();
RestartTimer=false;
}
私人空间开始工作()
{
TIFFtoXMLProcessor.EnqueueFileName(@“C:\test\yourtestfile1.txt”);
TIFFtoXMLProcessor.EnqueueFileName(@“C:\test\yourtestfile2.txt”);
TIFFtoXMLProcessor.EnqueueFileName(@“C:\test\yourtestfile3.txt”);
files=新列表(TIFFtoXMLProcessor.fileNamesQueue);
TIFFtoXMLProcessor.eventWaitHandle.Set();
if(TIFFtoXMLProcessor.worker.IsAlive==false)
{
foreach(文件中的var项)
{
System.IO.File.Delete(项目);
}
}
}
}
}
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
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统图;
使用System.IO;
命名空间WindowsFormsApp1
{
类文件处理器:IDisposable
{
public EventWaitHandle EventWaitHandle{get;private set;}
公共线程辅助程序{get;private set;}
私有只读对象锁定器=新对象();
公共队列fileNamesQueue{get;private set;}
公共字符串转换{get;private set;}
公共bool作业完成{get;private set;}
私有CancellationTokenSource cancelParallelWorker;
公共字符串颜色分隔符{get;private set;}
私有只读文本框状态;
公共字符串outputFolder{get;private set;}
列出文件目录;
//var AvailableJobsDictionary=新字典();
//字符串nZones、字符串zWidth、字符串fzWidth、字符串lzWidth、字符串zAreaWidth、字符串zAreaHeight、双DPI
公共文件处理器()
{
eventWaitHandle=新的自动重置事件(false);
fileNamesQueue=新队列();
//创建工作线程
worker=新线程(工作)
{
IsBackground=true
};
cancelParallelWorker=新的CancellationTokenSource();
worker.Start();
}
public void排队文件名(字符串文件名)
{
//将文件名排队
//此语句由lock保护,以防止其他线程在对文件名排队时弄乱队列
lock(locker)fileNamesQueue.Enqueue(FileName);
//向worker发送信号,表明文件名已排队,并且可以对其进行处理
//eventWaitHandle.Set();
}
私人工作()
{
List filesToWork=new List();
while(true)
{
字符串文件名=null;
//将文件名出列
锁(储物柜)
而(fileNamesQueue.Count>0)
{
fileName=fileNamesQueue.Dequeue();
filesToWork.Add(文件名);
如果(fileName==null)返回;
}
if(fileNamesQueue.Count==0&&filesToWork.Count>0)
{
var templast=新列表(filesToWork);
filesToWork.Clear();
ProcessJob(圣殿骑士);
}
}
}
私有void ProcessJob(列表文件工作)
{
尝试
{
JobComplete=true;
开关(电流转换)
{
案例“TIF”:
{
int j=0;
foreach(filesToWork中的var currJob)
{
//一系列的任务。。。
j++;
}
eventWaitHandle.WaitOne();
打破
}
}
JobComplete=false;
处置();
}
捕获(异常转换X)
{
cancelParallelWorker?.Cancel();
}
}   
#区域IDisposable成员
公共空间处置()
{
//向文件处理器发出退出信号
排队文件名(空);
//等待文件处理器的线程完成
worker.Interrupt();
//释放任何操作系统资源
eventWaitHandle.Close();
}
#端区
}
}

你的代码对于你所做的事情来说是极其复杂的
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
    }

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