How to create multiple threads in Windows azure worker role

时间秒杀一切 提交于 2019-12-18 11:33:50

问题


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


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5041153/how-to-create-multiple-threads-in-windows-azure-worker-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!