Execute a delegate in the ui thread (using message pump)

后端 未结 5 1647
抹茶落季
抹茶落季 2020-12-19 16:18

I have a background thread that handles communication with an external service. Each time the background thread receives a message I\'d like to pass it to the UI thread for

5条回答
  •  一向
    一向 (楼主)
    2020-12-19 16:58

    Here is an example of using the Dispacther object in WPF with MSMQ.

    The code behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Messaging;
    
    namespace MSMQGui
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
            private string queueName = @".\private$\MyFunWithMSMQ";
            private MessageQueue queue = null;
    
            public MainWindow()
            {
                InitializeComponent();
    
                if (!MessageQueue.Exists(queueName))
                    MessageQueue.Create(queueName,false);
    
    
               queue = new MessageQueue(queueName);
               queue.ReceiveCompleted += receiveCompleted;
    
            }
    
            private void btnAddMessage_Click(object sender, RoutedEventArgs e)
            {
                string message = txtMessage.Text;
                txtMessage.Text = String.Empty;
    
                queue.Send(message);
    
                MessageBox.Show("Message :" + message + " sent");
            }
    
            private void Populate(object sender, RoutedEventArgs e)
            {
                try
                {
                    queue.BeginReceive(TimeSpan.FromSeconds(1)) ;     
                }
                catch (MessageQueueException)
                {
                    MessageBox.Show("No message available");
                }
            }
    
            private void receiveCompleted(object source, ReceiveCompletedEventArgs e)
            {
                try
                {
                    var message=queue.EndReceive(e.AsyncResult);
    
    
    
                    Action addMessage= (string msg) => {
                         ListViewItem item = new ListViewItem();
                         item.Content = msg;
                         lsvMessages.Items.Add(item);
                    };
    
                    this.Dispatcher.Invoke(addMessage, message.Body as string);
                }
                catch (MessageQueueException)
                {
                    MessageBox.Show("No message available");
                }
            }
        }
    }
    

    The XAML:

    
        
            
                
                
            
    
            
                
                      
                
            
    
            
            
            
            
            
            
    
            
            
            
    
            
            
    
        
    
    

提交回复
热议问题