0

I have a WinForms C# .NET 4.5.2 OWIN self hosted server that uses a signalr hub. In this same WinForm project I start the signalr hub as follows:

private void Form1_Load(object sender, EventArgs e) {
        hubConnection = new HubConnection("http://localhost:12345/", clientParams);
        hubProxy = hubConnection.CreateHubProxy("OfmControl");
        hubProxy.On("Message", message => onData(message));
        hubConnection.Start();

My onData method looks like this:

private void onData(dynamic message)
{
    var file = new System.IO.FileInfo(message);
    playVideo(file.FullName);
}

playVideo method looks like this:

private void playVideo(string file)
{
    int tc6 = 0;

    try
    {
        axWMPn[0].URL = file;
    }
    catch (System.Runtime.InteropServices.COMException comEx)
    {
        Console.WriteLine("playVideo COMException 0: " + comEx.Source + "  -- " + comEx.Message);
    }
}

axWMPn is an activex Windows Media Player object on the main form. When I run a separate C# signalr client program and send a filename message to this OWIN signalr sever and onData receives the filename and assigns it to axWMPn[0] per above code it always works never hits catch exception never receive a cross thread exception? I have run it hundreds of times never once received a cross thread exception and always works? But if I do the following I receive a cross thread exception every time obviously:

private void onData(dynamic message)
{
    textBox1.text = message; ---> cross thread exception everytime here
    var file = new System.IO.FileInfo(message);
    playVideo(file.FullName);
}

I started thinking what I am doing is a cross thread violation but why does it always work why am I not receiving a thread violation in Visual Studio when I run the project in debug mode like I always do when I attempt to assign textBox1.text in onData? I have a feeling it has something to do with axWMP being an activex COM object but still seems I should eventually have a cross thread exception on this if it is?

If it is a cross thread violation do I need to do a BeginInvoke/Invoke around axWMPn[0] assignment? Thanks for any advice...

Neal Davis
  • 648
  • 6
  • 21
  • This is typical. The control runs on your win forms UI thread. While your hub is some background thread. This issue is a well-beaten one – T.S. Oct 09 '17 at 19:20
  • @T.S. - so in playVideo function you are saying it is a cross thread violation to assign axWMPn[0].URL = file; why does it always work and why am I not receiving a cross thread violation? Have I lucked out 100's of times? – Neal Davis Oct 09 '17 at 19:33
  • why don't you name the thread on which your form runs. Then check for the thread name in `onData`? I doubt that you will retrieve same thread name. Go from there – T.S. Oct 09 '17 at 19:40
  • @T.S. I believe you - I am certain they are different threads because as I said above I can't do textBox1.text = message; I always get cross thread exception in visual studio when I try that. I guess my primary question is why don't I get a cross thread exception on axWMPn[0].URL = file? Why does it work? – Neal Davis Oct 09 '17 at 20:04
  • May be, because it is not Winforms control, or, may be, because when you ser URL property, it doesn't cause any disturbance (i.e. firing UI events) like repainting, etc.. Interesting idea - try to set `textbox.Tag` property. Will that upset UI thread? – T.S. Oct 09 '17 at 20:25
  • @T.S. - I put textBox1.Tag = message in onData and it works just fine; so maybe essentially setting axWMPn[0].URL = message is a similar property of a control object that is only indirectly related to the GUI? Even though doing this does start the video file playing on my axWMP window? – Neal Davis Oct 09 '17 at 21:14
  • I understand it as this - a thread has to perform an operation when you set `.text`. It needs to fire some events but the thread is different from the one which sent notification. This is well-known fact that you need to use `BeginInvoke` for processing controls on different thread. So, there is nothing unusual there. In fact, this question has been around and arouns and around on SO https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the. In your case, you didn't realize that your object will run the method on different thread – T.S. Oct 09 '17 at 22:46

0 Answers0