C# 流程列表<;T>;在单独的线程中添加时的成员
首先,这里是一些我想要改进的伪代码 public List<ProcessData> Processes; System.Threading.Thread ProcessThread; void ProcessLoop() { while (true) { for (int i = 0; i < Processes.Count; i++) { if (HasPriority(Processes[i])) { Process(Processes[i]); } } System.Threading.Thread.Sleep(1000); } } void AddProcessData(ProcessData pd) { Processes.Add(pd); if (Suspended(ProcessThread)) Resume(ProcessThread); } void Startup() { ProcessThread = new System.Threading.Thread(ProcessLoop); ProcessThread.Start(); } 公开名单进程; System.Threading.Thread进程线程; void ProcessLoop() { while(true) { for(int i=0;iC# 流程列表<;T>;在单独的线程中添加时的成员,c#,visual-studio-2010,C#,Visual Studio 2010,首先,这里是一些我想要改进的伪代码 public List<ProcessData> Processes; System.Threading.Thread ProcessThread; void ProcessLoop() { while (true) { for (int i = 0; i < Processes.Count; i++)
或者使用计时器,并在经过的事件中进行检查。从Microsoft了解RX框架。 在那里,你可以每x秒获得一个事件,然后做一些事情。 根本不需要启动线程
或者使用计时器,并在经过的事件中进行检查。您想要的是一个事件 编辑:使其线程安全
public List<ProcessData> Processes;
System.Threading.Thread ProcessThread;
System.Threading.AutoResetEvent ProcessesChangedEvent = new System.Threading.AutoResetEvent(false);
void ProcessLoop()
{
while (true)
{
// You might want to copy out the entire list as an array instead
// if HasPriority or Process take a long time.
lock (Processes)
{
for (int i = 0; i < Processes.Count; i++)
{
if (HasPriority(Processes[i]))
{
Process(Processes[i]);
}
}
}
ProcessesChangedEvent.WaitOne(...); // timeout?
}
}
void AddProcessData(ProcessData pd)
{
lock (Processes)
Processes.Add(pd);
ProcessesChangedEvent.Set(); // you can also use Monitor.PulseAll/Wait
}
void Startup()
{
ProcessThread = new System.Threading.Thread(ProcessLoop);
ProcessThread.Start();
}
公共列表流程;
System.Threading.Thread进程线程;
System.Threading.AutoResetEvent ProcessChangedEvent=新的System.Threading.AutoResetEvent(false);
void ProcessLoop()
{
while(true)
{
//您可能希望将整个列表复制为数组
//如果有优先级或过程需要很长时间。
锁定(进程)
{
for(int i=0;i
您想要的是一个事件
编辑:使其线程安全
public List<ProcessData> Processes;
System.Threading.Thread ProcessThread;
System.Threading.AutoResetEvent ProcessesChangedEvent = new System.Threading.AutoResetEvent(false);
void ProcessLoop()
{
while (true)
{
// You might want to copy out the entire list as an array instead
// if HasPriority or Process take a long time.
lock (Processes)
{
for (int i = 0; i < Processes.Count; i++)
{
if (HasPriority(Processes[i]))
{
Process(Processes[i]);
}
}
}
ProcessesChangedEvent.WaitOne(...); // timeout?
}
}
void AddProcessData(ProcessData pd)
{
lock (Processes)
Processes.Add(pd);
ProcessesChangedEvent.Set(); // you can also use Monitor.PulseAll/Wait
}
void Startup()
{
ProcessThread = new System.Threading.Thread(ProcessLoop);
ProcessThread.Start();
}
公共列表流程;
System.Threading.Thread进程线程;
System.Threading.AutoResetEvent ProcessChangedEvent=新的System.Threading.AutoResetEvent(false);
void ProcessLoop()
{
while(true)
{
//您可能希望将整个列表复制为数组
//如果有优先级或过程需要很长时间。
锁定(进程)
{
for(int i=0;i
在您的情况下,最好的决定是使用下一个类中的一个:或在您的情况下,最好的决定是使用下一个类中的一个:或如果您真的只需要处理数据一次,以下是一些代码
public Queue<ProcessData> Processes;
System.Threading.Thread[] ProcessThreads = new System.Threading.Thread[5];
System.Threading.Semaphore semToDo = new System.Threading.Semaphore(0,int.MaxValue);
void ProcessLoop()
{
ProcessData pd;
while (true)
{
semToDo.WaitOne();
lock (Processes)
{
pd = Processes.Dequeue();
}
Process(pd);
}
}
private void Process(ProcessData processData)
{
throw new NotImplementedException();
}
void AddProcessData(ProcessData pd)
{
lock (Processes)
{
Processes.Enqueue(pd);
}
semToDo.Release();
}
void Startup()
{
//you can even have multiple worker threads now!
for(int i = 0; i < 5; i++)
ProcessThreads[i] = new System.Threading.Thread(ProcessLoop);
foreach (System.Threading.Thread t in ProcessThreads)
{
t.Start();
}
}
公共队列进程;
System.Threading.Thread[]ProcessThreads=新的System.Threading.Thread[5];
System.Threading.Semaphore semToDo=新的System.Threading.Semaphore(0,int.MaxValue);
void ProcessLoop()
{
过程数据pd;
while(true)
{
semToDo.WaitOne();
锁定(进程)
{
pd=processs.Dequeue();
}
过程(pd);
}
}
私有无效进程(ProcessData ProcessData)
{
抛出新的NotImplementedException();
}
void AddProcessData(ProcessData pd)
{
锁定(进程)
{
进程排队(pd);
}
semToDo.Release();
}
无效启动()
{
//现在你甚至可以有多个工作线程!
对于(int i=0;i<5;i++)
ProcessThreads[i]=新的System.Threading.Thread(ProcessLoop);
foreach(ProcessThreads中的System.Threading.Thread t)
{
t、 Start();
}
}
如果您真的只需要处理一次数据,下面是一些代码
public Queue<ProcessData> Processes;
System.Threading.Thread[] ProcessThreads = new System.Threading.Thread[5];
System.Threading.Semaphore semToDo = new System.Threading.Semaphore(0,int.MaxValue);
void ProcessLoop()
{
ProcessData pd;
while (true)
{
semToDo.WaitOne();
lock (Processes)
{
pd = Processes.Dequeue();
}
Process(pd);
}
}
private void Process(ProcessData processData)
{
throw new NotImplementedException();
}
void AddProcessData(ProcessData pd)
{
lock (Processes)
{
Processes.Enqueue(pd);
}
semToDo.Release();
}
void Startup()
{
//you can even have multiple worker threads now!
for(int i = 0; i < 5; i++)
ProcessThreads[i] = new System.Threading.Thread(ProcessLoop);
foreach (System.Threading.Thread t in ProcessThreads)
{
t.Start();
}
}
公共队列进程;
System.Threading.Thread[]ProcessThreads=新的System.Threading.Thread[5];
System.Threading.Semaphore semToDo=新的System.Threading.Semaphore(0,int.MaxValue);
void ProcessLoop()
{
过程数据pd;
while(true)
{
semToDo.WaitOne();
锁定(进程)
{
pd=processs.Dequeue();
}
过程(pd);
}
}
私有无效进程(ProcessData ProcessData)
{
抛出新的NotImplementedException