我有一个场景,我有多个线程添加到队列和多个线程从同一队列读取.如果队列达到特定大小,则填充队列的所有线程将在添加时被阻止,直到从队列中删除项目为止.
下面的解决方案就是我现在正在使用的问题,我的问题是:如何改进?是否有一个对象已经在我应该使用的BCL中启用此行为?
internal class BlockingCollection<T> : CollectionBase, IEnumerable
{
//todo: might be worth changing this into a proper QUEUE
private AutoResetEvent _FullEvent = new AutoResetEvent(false);
internal T this[int i]
{
get { return (T) List[i]; }
}
private int _MaxSize;
internal int MaxSize
{
get { return _MaxSize; }
set
{
_MaxSize = value;
checkSize();
}
}
internal BlockingCollection(int maxSize)
{
MaxSize = maxSize;
}
internal void Add(T item)
{
Trace.WriteLine(string.Format("BlockingCollection add waiting: {0}", Thread.CurrentThread.ManagedThreadId));
_FullEvent.WaitOne();
List.Add(item);
Trace.WriteLine(string.Format("BlockingCollection …Run Code Online (Sandbox Code Playgroud) 我在空闲时间一直在使用网络爬行.NET应用程序,我想要包含的这个应用程序的一个功能是暂停按钮来暂停特定的线程.
我对多线程比较陌生,我无法找到一种无限期暂停线程的方法.我不记得确切的类/方法,但我知道有一种方法可以做到这一点,但它被.NET框架标记为已过时.
有没有什么好的通用方法可以无限期地暂停C#.NET中的工作线程.
我最近没有花很多时间来处理这个应用程序,最后一次触及它是在.NET 2.0框架中.我对.NET 3.5框架中存在的任何新功能(如果有的话)持开放态度,但是我想知道在2.0框架中也能工作的解决方案,因为这是我在工作中使用的,这对于知道以防万一.
我在C#中开发一个软件,它通过Wrapper使用C++ .dll文件中的静态函数.
问题是这些函数中的一些是缓慢且不稳定的,所以为了解决这个问题,我创建了一个执行它们的Thread.但是,当我从主线程中止该线程时,程序不允许我再次使用这些函数,即使我每次调用函数时都定义了一个新的线程实例.
有什么办法可以解决这个问题吗?
提前致谢.
PS:这是我的程序中的一些代码:
public partial class MainForm : Form, IMultiLanguage
{
//...
//This method is subscribed to the event of pressing the 'Abort' button
private void StopCurrentAnalisis()
{
try
{
this.analisisManagerController.AnalisisThread.Abort();
}
catch (Exception e){ }
finally
{
MessageBox.Show("Analisis has been cancelled by user", "Analisis Interrupted", MessageBoxButtons.OK, MessageBoxIcon.Stop);
CerrarNowLoadingForm();
}
}
//..
}
public class AnalisisManager: IAnalisisManagerController
{
//..
private Thread analisisThread;
public Thread AnalisisThread{get{return this.analisisThread;}}
public void MakePadrobAnalisis(TipoAnalisis tipoAnalisis,
Dictionary<string, Dictionary<int, double>> parametros)
{
object[] arregloParams …Run Code Online (Sandbox Code Playgroud)