locked
Using A Task to BeginPeek From a MessageQueue RRS feed

  • Question

  • Hello Everyone,

    I am trying to use the Visual Studion Async Functions to peek at a MSMQ.

    Has anyone got any examples of how to do this.

    I created an action

     

            public Action<object> action = (object obj) =>
            {
                Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId);
            };

    Access the queue

    queue = new MessageQueue(@".\private$\servertaba");

    msgqueueCurrentCursor = queue.CreateCursor();

    Task ts = Task.Factory.FromAsync(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Next, 0, null), action);

    ts.Wait();
    IAsyncResult = (IAsyncResult) (ts);

    Message msg = queue.EndPeek(result);

    Console.WriteLine(msg.Id);

    This does not work.

    Any examples of how to accomplish this would be great.

    Thanks in advance

     

    Angelo Laris


    Sunday, February 5, 2012 6:45 AM

Answers

  • The idea behind TaskFactory.FromAsync is that it manages the whole lifetime of the asynchronous operation. The Action delegate you pass should call End*.

    var queue = new MessageQueue(@".\private$\servertaba");
    var msgqueueCurrentCursor = queue.CreateCursor();
    
    var task = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Next, 0, null), queue.EndPeek);
    var message = await task;
    
    Console.WriteLine(message.Id);
    

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:00 AM
    • Unmarked as answer by Angelo.Laris Monday, February 6, 2012 11:00 AM
    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:09 AM
    Sunday, February 5, 2012 2:51 PM
  • Thank you Steve.

    I include the code that makes this work and is tested

                Task<Message> ts = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Current, 0, null), queue.EndPeek);
                ts.Wait();
                Message msg = ts.Result;
                Console.WriteLine(msg.Id.ToString());
                while (true)
                {
                    Task<Message> tsnext = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Next, 0, null), queue.EndPeek);
                    tsnext.Wait();
                    Message msg = tsnext.Result;
                    Console.WriteLine(msg.Id.ToString());
                }

     

     

    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:09 AM
    Monday, February 6, 2012 11:09 AM

All replies

  • The idea behind TaskFactory.FromAsync is that it manages the whole lifetime of the asynchronous operation. The Action delegate you pass should call End*.

    var queue = new MessageQueue(@".\private$\servertaba");
    var msgqueueCurrentCursor = queue.CreateCursor();
    
    var task = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Next, 0, null), queue.EndPeek);
    var message = await task;
    
    Console.WriteLine(message.Id);
    

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:00 AM
    • Unmarked as answer by Angelo.Laris Monday, February 6, 2012 11:00 AM
    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:09 AM
    Sunday, February 5, 2012 2:51 PM
  • Thank you Steve.

    I include the code that makes this work and is tested

                Task<Message> ts = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Current, 0, null), queue.EndPeek);
                ts.Wait();
                Message msg = ts.Result;
                Console.WriteLine(msg.Id.ToString());
                while (true)
                {
                    Task<Message> tsnext = Task.Factory.FromAsync<Message>(queue.BeginPeek(new TimeSpan(1, 0, 0, 1, 0), msgqueueCurrentCursor, PeekAction.Next, 0, null), queue.EndPeek);
                    tsnext.Wait();
                    Message msg = tsnext.Result;
                    Console.WriteLine(msg.Id.ToString());
                }

     

     

    • Marked as answer by Angelo.Laris Monday, February 6, 2012 11:09 AM
    Monday, February 6, 2012 11:09 AM
  • Is there a way to do this using either a distributed transaction or a message queue transaction so that I only pull the message off the queue if it is successfully handled (i.e. written to a database)?

    Thank you!

    Saturday, September 22, 2012 9:39 PM