How to access UI thread for my WCF subscriber?

I am currently developing a WCF publishing publishing service. The subscriber is a winform application. Since the subscriber needs to implement a callback method for the service, which in my case is the PostReceived () method, and the publisher has the PublishPost () method.

For the PostReceived () method for my winform, it cannot access the winform UI thread. The subscription method is done according to my main method. How to program the PostReceived () method so that it can access labels and those for my main form?

EDIT

what I tried so far calls the mainForm object from my program.cs, but it crashes when I run all 3, indicating an error that it cannot access the user interface thread.

EDIT 2

I tried using the following code, but there is an error for it.

mainForm b; public void PostReceived(string postSampleData) { b.BeginInvoke((MethodInvoker)delegate() { b.lblSearch.Text = "lakjslkaja"; }); 

After running the code, an error appears

 Object reference not set to an instance of an object. 

Any idea how to fix this?

+4
source share
1 answer

Your PostReceived method should be something like this.

 void PostReceived() { yourform.BeginInvoke((MethodInvoker)delegate() { yourform.button.Text = "new label"; //More stuff here }); } 

This ensures that all content after BeginInvoke is invoked in the user interface thread.

+4
source

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


All Articles