0

I have created a new winforms application. I am attempting to Create a class that listens to a storage queue for messages.

 using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using System.Threading;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Configuration;

namespace PMonitor
{
         public class QueueWorker : RoleEntryPoint
    {

        public override void Run()
        {
               CloudQueue queue = clnt.GetQueueReference("stuff");

        while (true)
        {
            CloudQueueMessage qMessage = null;

            do
            {
                qMessage = queue.GetMessage(TimeSpan.FromSeconds(10));
                if (qMessage != null)
                {
                   //handle message
                }
            }
            while (qMessage != null);


            Thread.Sleep(10000);     
            }
        }

        public override bool OnStart()
        {

            return base.OnStart();
        }

    }

}

How do I engage the worker to start running in the Form to run and tell me when it finds a message in the queue?

Currently instantiating this class and calling Run() in the Form Load locks up the form.

What is the pattern for doing this?

nlstack01
  • 789
  • 2
  • 7
  • 30
  • I'm curious to know why you want to use a worker role in your WinForm application. Would you mind describing your use case? – Gaurav Mantri Feb 14 '16 at 07:34
  • All I am trying to do is handle a message when this code see one on the queue. If I call this class in the form load it locks up the Form. I must need an async pattern to get a message so the form doesnt lock up? – nlstack01 Feb 14 '16 at 16:33

1 Answers1

1

I suspect there's a basic misunderstanding about worker roles. These are not services that you instantiate like a class. Rather, these are definitions for stateless virtual machines that run in Azure. The code snippet you posted is the skeleton code that gets run after a worker role instance (a VM) is booted up, with code (such as queue consumption, in your case) running within the Run() method.

A WinForms application have zero need for RoleEntryPoint classes, since they are exclusively used within the stateless VM's running in Azure.

Queue processing (or any other tasks you might need to run) have no ties to worker roles; your app can consume queue messages just by working with the storage SDK (or by calling the REST API directly). How/where you set up your queue-processing logic is completely up to you.

Note: You can certainly call services running within worker role instances, from your WinForms application, but I don't think that's what you're asking.

There are several answers on StackOverflow which go into more detail about worker roles, such as this one.

Community
  • 1
  • 1
David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • All I am trying to do is handle a message when this code see one on the queue. If I call this class in the form load it locks up the Form. I must need an async pattern to get a message so the form doesnt lock up? – nlstack01 Feb 14 '16 at 16:33
  • I don't think you're understanding what I explained. You cannot use the code you're trying to use locally in your desktop application. You cannot call that class from your form load, regardless whether you use async. – David Makogon Feb 14 '16 at 16:37