如何在Windows azure worker角色中创建多个线程

时间:2011-02-18 12:15:42

标签: .net azure azure-worker-roles

我想在单个辅助角色中执行多个操作。如何以worker角色创建线程?

3 个答案:

答案 0 :(得分:5)

您可以按http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2010/12/running-multiple-threads-on-windows.html

中所述的WorkerRole::OnStart()添加多个工作人员
    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);
            }
        }
    }

答案 1 :(得分:4)

简而言之,它与其他任何控制台应用程序没有什么不同。

答案 2 :(得分:1)

执行此操作的两个不同示例: