C#轻松地在线程之间传递数据

时间:2017-02-13 13:59:37

标签: c# wpf zeromq

我目前正在开展一个项目,我将框架作为bytearray接收并在GUI(WPF)上显示。现在我对性能不满意,因为帧显示延迟。 所以我考虑做多线程,以便套接字和GUI彼此独立工作。但是,如果我尝试在单独的线程中运行socketRoutine我得到一个错误,一个线程无法访问另一个线程的ressource。

我不确定哪个资源是什么意思。我想它的字节数传递给GUI或者它必须被访问的GUI组件。

这些是我现在的代码。

namespace Subscriber_WPF{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 

public partial class MainWindow : Window{

    /*Kinect Datentypen*/
    private WriteableBitmap colorBitmap = null;
    static readonly String publisherID = "TYPE1";

    SocketHandler socketHandler;
    Thread socketThread;

    public MainWindow(){     
        ConsoleManager.Show();
        colorBitmap = new WriteableBitmap(1920, 1080, 96.0, 96.0, PixelFormats.Bgra32, null);
        this.DataContext = this;

        //initializeKinectComponents();
       socketHandler = new SocketHandler(5555, publisherID, this);
       socketThread = new Thread(()=>socketHandler.runSocketRoutine());

        Console.WriteLine("GUI-Components initialized. Press Enter to start receiving Frames.");

        this.KeyDown += MainWindow_KeyDown;

    }

    private void MainWindow_KeyDown(object sender, KeyEventArgs e){
        if (e.Key.Equals(Key.Return)) {
            socketThread.Start();
        }
    }

    public void refreshGUI(byte[]content) {
       Action EmptyDelegate = delegate () { };
       BitmapSource source = BitmapSource.Create(1920, 1080, 72, 72, PixelFormats.Bgra32, BitmapPalettes.Gray256, content, 1920 * 4);
       videoView.Source = source;

        /*interrupt the socket-Loop to update GUI=> that is the current method without multithreading*/
       //this.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);

    }


}

的SocketHandler

namespace Subscriber_WPF{

class SocketHandler{

    private int port;
    private string publisherID;
    private MainWindow window;
    private static ZContext context;
    private static ZSocket subscriber;

    public SocketHandler(int port, string publisherID, MainWindow window) {
        this.port = port;
        this.publisherID = publisherID;
        this.window = window;
        this.initializeZMQSocket(this.port, this.publisherID);
    }

    private void initializeZMQSocket(int port, String publishID){
        context = new ZContext();
        subscriber = new ZSocket(context, ZSocketType.SUB);
        /*initialize sockets*/
        subscriber.Connect("tcp://127.0.0.1:" + port);
        subscriber.Subscribe(publishID);
        Console.WriteLine("subscriber is ready!");
    }


    public void runSocketRoutine(){
        Console.WriteLine("Waiting for Messages.");

        while (true)
        {
            byte[] content = new byte[8294400];

            using (ZMessage message = subscriber.ReceiveMessage())
            {
                Console.WriteLine("Message received!");
                string pubID = message[0].ReadString();
                /**/
                if (pubID.Equals(publisherID))
                {

                    content = message[1].Read();
                    Console.WriteLine("size of content: " + message[1].Length);
                    window.refreshGUI(content);
                }

            }
        }

    }


}

如果有人知道如何停止延迟显示或我如何轻松处理该线程问题,我将非常感激!

许多问候!

1 个答案:

答案 0 :(得分:2)

我过去使用优秀的RX framework做过类似的事情。您的运行套接字例程变为IObservable,其中T是您要返回的类型,在您的情况下为byte []。然后,您可以在UI线程上订阅此流。使用RX框架你也可以做一些很酷的事情,比如速率限制,所以如果你的例程吐出很多“记录”,你可以将它限制在每个时间段1个。

相关问题