I want to do multiple operations in a single worker role. How to create threads in worker role?
How to create multiple threads in Windows azure worker role
Asked Answered
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);
}
}
}
Can you provide a reference for your claim that Azure will kill threads after 30s? In this Microsoft example they are creating threads in Role.Run(). –
Arianna
This may be a problem that I have with my environment that causes an unhandled exception that crashes my threads. –
Keturahkeung
In a nutshell, it's no different then in any other console application.
Actually, this is false. Creating threads using Task.Factory or Thread.Start within a Azure Worker role is bad as Azure will kill the thread after about 30 seconds. –
Keturahkeung
Inge, sorry, but I pretty sure you're wrong. At Lokad, we have been in production for 2 years now on Azure, and Lokad.Cloud (code.google.com/p/lokad-cloud), the underlying framework, extensively use
Thread.Start
to spin new threads. –
Crippling Yep, and Azure computing istance doesn't inherently kill any threads. Long running SQL Azure connections however may very well be killed. –
Cuttlebone
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.
An updated link would be this - msdn.microsoft.com/en-us/library/ff803365.aspx (5 – Executing Background Tasks) –
Dara
© 2022 - 2024 — McMap. All rights reserved.