【发布时间】:2013-08-03 12:51:42
【问题描述】:
我想知道在我的函数结束后如何中止我的线程Thread.Abort();
我的应用程序运行文件和打开的每个文件都是不同的线程
int _counter;
int _parallelThreads
_queue = new Queue();
public void transmit()
{
while (_counter < _parallelThreads)
{
lock (_queue)
{
string file = (string)_queue.Dequeue();
ThreadStart ts = delegate { processFile(file); };
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start();
_counter++;
}
}
}
private void processFile(string file)
{
WiresharkFile wf = new WiresharkFile(file, _selectedOutputDevice, 1);
wf.OnFinishPlayEvent += wf_OnFinishPlayEvent;
wf.sendBuffer();
}
这是我的文件完成的事件
private void wf_OnFinishPlayEvent(MyClass class)
{
// here i want to abort my thread
}
我想在线程完成后中止线程的原因是因为我认为这是我的内存不足的原因,以防我打开很多并行线程并反复运行它(我的应用程序内存使用量读取超过 1 giga)
【问题讨论】:
-
你真的不应该中止线程。通过检查标志或其他方式,提供一种在不再需要时优雅地退出它们的方法。
-
为什么中止线程是个坏主意?像你建议的那样中止和退出有什么区别?
-
This answer 很好地解释了 Thread.Abort() 的核心问题。
-
_parallelThreads的值是多少? -
同时打开多少个线程,但我内存不足,我知道因为我打开了所有线程