我想在单个辅助角色中执行多个操作。如何以worker角色创建线程?
答案 0 :(得分:5)
您可以按http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html
中所述的WorkerRole::OnStart()
添加多个工作人员
public class WorkerRole : ThreadedRoleEntryPoint
{
public override void Run()
{
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("Worker Role entry point called", "Information");
base.Run();
}
public override bool OnStart()
{
List<WorkerEntryPoint> workers = new List<WorkerEntryPoint>();
workers.Add(new ImageSizer());
workers.Add(new ImageSizer());
workers.Add(new ImageSizer());
workers.Add(new HouseCleaner());
workers.Add(new TurkHandler());
workers.Add(new Crawler());
workers.Add(new Crawler());
workers.Add(new Crawler());
workers.Add(new Gardener());
workers.Add(new Striker());
return base.OnStart(workers.ToArray());
}
}
internal class Striker : WorkerEntryPoint
{
public override void Run()
{
while (true)
{
// Do Some Work
Thread.Sleep(100);
}
}
}
答案 1 :(得分:4)
简而言之,它与其他任何控制台应用程序没有什么不同。
答案 2 :(得分:1)
执行此操作的两个不同示例:
http://msdn.microsoft.com/en-us/library/ff803372.aspx(滚动到“实施内部”
http://msdn.microsoft.com/en-us/library/ff966485.aspx(滚动到“实施内部”)。此示例使用.NET 4.0中提供的TPL进行并行任务调度。