调用线程中的方法

时间:2012-03-30 18:05:52

标签: c# multithreading invoke eventargs

我无法使用触发第二个线程的方法中的eventArgs:

 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 });
            .
            .
            .
        }
    }   
}

工作正常,但是当我尝试在用户方法(主线程)中使用“sender”和“e”事件时,会出现以下消息: “调用线程无法访问此对象,因为另一个线程拥有它。”

有人可以给我一个关于这个问题的提示吗?谢谢,

Jeferson


跟随都铎,再次感谢。此代码位于window.xaml.cs代码中。第一篇文章中的代码位于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();
}

3 个答案:

答案 0 :(得分:0)

如果你的申请是winforms,你可以这样做

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

    private void foo(int a, int b)
    {

    }

在此示例中,dfoo位于表单的类

答案 1 :(得分:0)

标签更新需要通过Dispatcher.BeginInvoke完成:

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

答案 2 :(得分:0)

非常感谢,这很好用

if (this.InvokeRequired)
{
  BeginInvoke(new MethodInvoker(delegate()
  {
    printausfueren();
  }));
}
else
{
     printausfueren();
}