I use code similar to this, but this is not working, becouse client subscibe callback action is running in non UI thread. How can I schedule callback action to windows forms UI thread?
internal static class Program
{
[STAThread]
private static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var context = new ProgramApplicationContext())
{
Application.Run(context);
}
}
}
internal class ProgramApplicationContext : ApplicationContext
{
public ProgramApplicationContext()
{
...
_client.subscribe("event", message => {
var form = new CommunicationForm();
form.Show();
});
}
}
>Solution :
You can use SynchronizationContext to create the form on the UI thread.
public ProgramApplicationContext()
{
...
var ctx = SynchronizationContext.Current;
_client.subscribe("event", message => {
ctx.Send(state => {
var form = new CommunicationForm();
form.Show();
}, null);
});
}