I'm currently working on a project where I receive frames as bytearray and display them on the GUI (WPF). Right now im unsatisfied with the performance, because the frames are getting displayed delayed. So I thought about doing Multithreading, so that the socket and the GUI are working independent from each other. However If I try to run the socketRoutine in a seperate Thread I get an error, that a thread cant access the ressource of the other thread.
Im not sure which ressource is meant. I guess its either the bytearray im passing to the GUI or its the GUI component which has to be accessed.
These is my code right now.
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);
}
}
}
}
}
If someone has an idea on how to stop that delaying display or how i could easily handle that Thread-issue I would be very grateful!
Many Greets!