Beispiel #1
0
 void MainWindow_Loaded(object sender, RoutedEventArgs e)
 {
     try
     {
         var messageService = new MessageService();
         messageService.Insert(new MessageInfo { Id = 1, Content = "Hello, MSMQ" });
     }
     catch (System.Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex);
     }
 }
Beispiel #2
0
        private static void ProcessMessages()
        {
            TimeSpan tsTimeout = TimeSpan.FromSeconds(Convert.ToDouble(transactionTimeout * batchSize));
            MessageService messageService = new MessageService();

            while (true)
            {
                TimeSpan datetimeStarting = new TimeSpan(DateTime.Now.Ticks);
                double elapsedTime = 0;
                int processedItems = 0;
                ArrayList queueMessages = new ArrayList();

                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tsTimeout))
                {
                    for (int j = 0; j < batchSize; j++)
                    {
                        try
                        {
                            if ((elapsedTime + queueTimeout + transactionTimeout) < tsTimeout.TotalSeconds)
                            {
                                queueMessages.Add(messageService.ReceiveFromQueue(queueTimeout));
                            }
                            else
                            {
                                j = batchSize;   // exit loop
                            }
                            //update elapsed time
                            elapsedTime = new TimeSpan(DateTime.Now.Ticks).TotalSeconds - datetimeStarting.TotalSeconds;
                        }
                        catch (TimeoutException)
                        {
                            //exit loop because no more messages are waiting
                            j = batchSize;
                        }
                    }

                    for (int k = 0; k < queueMessages.Count; k++)
                    {
                        messageService.Insert((MessageInfo)queueMessages[k]);
                        processedItems++;
                        totalMessagesProcessed++;
                    }

                    //batch complete or MSMQ receive timed out
                    ts.Complete();
                }
                Debug.WriteLine("(Thread Id " + Thread.CurrentThread.ManagedThreadId + ") batch finished, " + processedItems + " items, in " + elapsedTime.ToString() + " seconds.");
            }
        }