How to create multiple threads in Windows azure worker role
I want to do multiple operations in a single worker role. How to create threads in work开发者_开发问答er role?
You could add multiple workers in the WorkerRole::OnStart()
as described here http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html
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);
}
}
}
In a nutshell, it's no different then in any other console application.
Two different examples that do this:
http://msdn.microsoft.com/en-us/library/ff803372.aspx (scroll to "Inside the implementation"
http://msdn.microsoft.com/en-us/library/ff966485.aspx (scroll to "inside the implementation"). This example uses the TPL available in .NET 4.0 for parallel task scheduling.
精彩评论