Perform delegation in ui stream (using message pump)

I have a background thread that processes communication with an external service. Each time the background thread receives a message, I would like to pass it to the user interface thread for further processing (display to the user).

Currently, I have created a message queue with threads that periodically group in Timer.Tick and fill up with a background thread. But this solution is sub optimal.

Do you know how to use a message pump to send events from a background thread to a ui stream?

+3
source share
5 answers

There are several methods.

+4

GUI , Application.DoEvents, GUI .

, Control.BeginInvoke Control.Invoke, , Control.Invoke , Control .

0

WPF ( ) WindowsBase.dll.

0

Dispacther WPF MSMQ.

:

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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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<string> 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");
            }
        }
    }
}

XAML:

<Window x:Class="MSMQGui.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="3*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="1*"></RowDefinition>
            <RowDefinition Height="9*"></RowDefinition>      
            <RowDefinition Height="1*"></RowDefinition>
        </Grid.RowDefinitions>

        <!-- First row -->
        <Label x:Name="lblMessage" 
               Content="Message:" 
               HorizontalAlignment="Stretch" 
               VerticalAlignment="Top" 
               HorizontalContentAlignment="Right"
               Grid.Column="0" Grid.Row="0"
               ></Label>
        <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal">
        <TextBox x:Name="txtMessage"  Width="200" HorizontalAlignment="Left" ></TextBox>
        <Button x:Name="btnAddMessage"  Content="Add message" Margin="5,0,0,0" Click="btnAddMessage_Click"></Button>
        </StackPanel>

        <!-- Second row -->
        <ListView x:Name="lsvMessages" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,5,0,0">
        </ListView>

        <!-- Third row-->
        <Button x:Name="btnPopulate" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Right" Click="Populate" Content="Get messages from queque" Margin="5,0,0,0"></Button>

    </Grid>
</Window>
0

Source: https://habr.com/ru/post/1712688/


All Articles