How to create multiple threads in Windows azure worker role

Ashwani K picture Ashwani K · Feb 18, 2011 · Viewed 7.8k times · Source

I want to do multiple operations in a single worker role. How to create threads in worker role?

Answer

user152949 picture user152949 · Jun 17, 2011

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);
            }
        }
    }