How to create multiple threads in Windows azure worker role
Asked Answered
T

3

8

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

Thereof answered 18/2, 2011 at 12:15 Comment(0)
K
5

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);
            }
        }
    }
Keturahkeung answered 17/6, 2011 at 0:16 Comment(2)
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
C
4

In a nutshell, it's no different then in any other console application.

Cuttlebone answered 18/2, 2011 at 17:23 Comment(3)
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
M
1

Two different examples that do this:

Mede answered 23/2, 2011 at 4:6 Comment(1)
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.