因此,我们正在使用 Windows azure 设置一个工作角色,它以非常高的 CPU 利用率运行。我认为这与本节有关,但我不确定该怎么做。每个启动的单独线程都有自己的睡眠,但主线程只是在一个 while 循环中运行。不应该在那里睡觉吗?
public class WorkerRole : RoleEntryPoint
{
private List<ProcessBase> backgroundProcesses = new List<ProcessBase>();
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning);
}
}
while (true) { }
}
这样的事情怎么样,这样合适吗?
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("BackgroundProcesses entry point called", "Information");
List<Task> TaskList = new List<Task>();
foreach (ProcessBase process in backgroundProcesses)
{
if (process.Active)
{
TaskList.Add(Task.Factory.StartNew(process.Run, TaskCreationOptions.LongRunning));
}
}
Task.WaitAll(TaskList.ToArray());
//while (true) { }
}