解决方案 »
- 一个很简单的问题
- [有难度!]windows有没有对应用程序的打开和关闭情况进行记录?没有的话用C#怎么干?
- 一个超级难的问题,请高手提供思路
- 鼠标放到窗体上会变色,怎么实现
- 如何实现局域网之间的文件上传下载
- C# Web Service引用COM
- 求一个10亿条以上数据的分页过程
- 如何确定ContextMenuStrip是在哪个控件上打开的?
- 如何通过当前的System.Windows.Forms.DataGridTextBox 对象获得当前的DataGridCell对象?
- 请教:.aspx中,html的table转换为服务器控件后,如何再加上一个服务器控件tbody
- 请问 == 和 值类型有什么关系呀?
- WPF,这是什么异常
{
private Dictionary<string, bool> _ThreadList = new Dictionary<string, bool>();
publice List<string> TypeList=new List<string>();
publice ThreadTest()
{
//初始化类型
TypeList.Add("A");
TypeList.Add("B");
TypeList.Add("C");
}
public void test(string pType)
{
_ThreadList[pType] = true;//表示正在处理 /*这里是每一个线程的处理内容 ,我是让他们分开执行,比如,
类型A在执行的时候,类型B要执行,不需要等待A执行,
而是另开一个空间给B去执行,不然的话,B执行有时会覆盖A类型数据的内容,
目的是让他们几个类型的线程能同时执行~互不二扰,要怎么做呢?
希望各位帮我一忙,谢谢了*/
_ThreadList[pType] = false;//表示处理完成
}
publice void RunTest()
{
//TypeList.Count最大是20;
for (int i = 0; i < TypeList.Count; i++)
{
string _type = TypeList[i];
if (!_ThreadList.ContainsKey(_type))
{
_ThreadList.Add(_type, false);
}
if(_ThreadList[_type]) continue;//如果线程类型在执行,则跳过。在上面少了这一行
Thread _t = new Thread(new ParameterizedThreadStart(test));
_t.Priority = ThreadPriority.Lowest;
_t.IsBackground = true;
_t.Start(_type);
}
}
}
@caozhy
你看我上面的是不是类似于 生产者 消费者,_ThreadList就是一个队列的管理,但是我要怎么做才能做到 A在执行时,B也要执行,但是执行的方法我怎么用队列来分开执行这些方法,或者是有没有办法 做一个方法队列呢?
坏处,或者说好处是,对初学者来说它隐藏了一些同步细节。
不过可以肯定的是它的代码比较简单:class Test
{
static void Main()
{
const int MaxWorkerCount = 2;
for (int i = 0; i < MaxWorkerCount; i++)
{
ThreadPool.QueueUserWorkItem(WorkerThread);
} Console.WriteLine("空行退出...");
string str;
while ((str = Console.ReadLine()) != "")
{
workingItems.Add(str);
} workingItems.CompleteAdding();
Thread.Sleep(3000);
} static BlockingCollection<string> workingItems = new BlockingCollection<string>();
static void WorkerThread(object state)
{
foreach (string str in workingItems.GetConsumingEnumerable())
{
Console.WriteLine("T{0} processing {1}...", Thread.CurrentThread.ManagedThreadId, str);
Thread.Sleep(2000);
}
Console.WriteLine("T{0} ended.", Thread.CurrentThread.ManagedThreadId);
}
}