Question

I can't use eventArgs from a method trigged for a second Thread:

 public class MovilinkCommunication
 {
    //Method Declarations
    public delegate void MovilinkWatchParametersEventMethod(ParameterAddress sender, MovilinkEventArgs e);
    private MovilinkWatchParametersEventMethod onWatchParameterMethod;

    //class constructor
    //here, the user inputs the method (in main thread) that desires to call in
    //parameter changed moment
    public MovilinkCommunication(MovilinkWatchParametersEventMethod userOnWatchParameterMethod)
    {
        //assign user method (in main thread) to wach variables
        onWatchParameterMethod = userOnWatchParameterMethod;

        //start communication thread (second thread)
        Thread movilinkThread = new Thread(new ThreadStart(movilinkIOManagerThread));
        movilinkThread.Start();
    }
    .
    .
    .
    //create delegates with "sender" parameter and "e" conditions of call
    delegate void CallOnWatchParameterMethod(ParameterAddress sender, MovilinkEventArgs e);
    private void callOnWatchParameterMethod(ParameterAddress sender, MovilinkEventArgs e) 
    { 
        //calling user method in main thread with event args obtained in
        //communication thread (second thread)
        onWatchParameterMethod(sender, e); 
    }
    .
    .
    .
    //communication thread
    private void movilinkIOManagerThread()
    {
        ParameterAddress sender;
        MovilinkEventArgs e;
        .
        .
        .
        while (movilinkAccessor.OperationStatusOk)
        {
            .
            .
            .
            CallOnWatchParameterMethod thdCallOnWatchParameterMethod =
               new CallOnWatchParameterMethod(callOnWatchParameterMethod);

            Dispatcher.CurrentDispatcher.Invoke(thdCallOnWatchParameterMethod, new object[] { sender, e });
            .
            .
            .
        }
    }   
}

Works fine, but when I try use "sender" and "e" event args in user method (in main thread), the message bellow appears: "The calling thread cannot access this object because a different thread owns it."

Can someone give me a hint about this problem? Thanks,

Jeferson


Follow Tudor, thanks again. This code is in window.xaml.cs code. The code in first post is in MovilinkComunication.cs.

MovilinkCommunication comunicadorMovilink;
private void wndPrincipal_Loaded(object sender, RoutedEventArgs e)
{
    //creating communication object, setting the desired event
    //to be trigged in secundary thread
    comunicadorMovilink = 
        new MovilinkCommunication(getChangeParameters_Movilink);
}        
.
.
.
//desired method to made actions in window, if detected
//change of parameters in external hardware
private void getChangeParameters_Movilink(ParameterAddress sender, MovilinkEventArgs e)
{
    //error occurs here. Any code with GUI return error.
    label24.Content = e.ActualValue.ToString();
}
Was it helpful?

Solution

The label update needs to be done via Dispatcher.BeginInvoke:

private void getChangeParameters_Movilink(ParameterAddress sender, MovilinkEventArgs e)
{
    label24.Dispatcher.BeginInvoke(
       (Action)(() =>
       {
          label24.Content = e.ActualValue.ToString();
       }));
}

OTHER TIPS

if your application is winforms, you can do this

    public void d()
    {
        if (this.InvokeRequired)
        {
            BeginInvoke( new MethodInvoker( delegate() { 
                foo(a, b); 
            } ) );
        }
        else
        {
            foo(a, b);
        }
    }

    private void foo(int a, int b)
    {

    }

in this example, d and foo are located in the form's class

Thanks a lot, this works fine

if (this.InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    printausfueren();
  }));
}
else
{
     printausfueren();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top