【问题标题】:ICommand exception, thread cannot access the objectICommand 异常,线程无法访问对象
【发布时间】:2016-02-13 14:24:48
【问题描述】:

我使用这个类来启用和禁用网络时的按钮 已连接我在 Gpfgateway 类中发起一个事件以通知网络是 已连接并且按钮将被禁用,当我在 在我断开或连接网络后乞讨工作抛出一个 例外。 Gpfgateway 中的事件处理程序是 Thread。

例外:

windowsBase.dll 中的 System.InvalidoperationException 附加信息:调用线程无法访问它,因为 不同的线程拥有它。

参考这行代码:

 CanExecuteChanged(this, new EventArgs())

代码:

public class NewAnalysisCommand : ICommand
{
    private AnalysisViewModel analysisViewModel = null;
    private Measurement measurement;


    public NewAnalysisCommand(AnalysisViewModel viewAnalysis)
    {
        analysisViewModel = viewAnalysis;
        GpfGateway.GetInstance().SystemStatus += updateCanExecuteChanged;
    }

    /// <summary>Notifies command to update CanExecute property.</summary>
    private void updateCanExecuteChanged(object sender, EventArgs e)
    {
        CanExecuteChanged(this, new EventArgs());
    }
    bool ICommand.CanExecute(object parameter)
    {
        return GpfGateway.GetInstance().IsConnected;
    }

    public event EventHandler CanExecuteChanged;
    void ICommand.Execute(object parameter)
    { 
        NewAnalysisViewModel newAnalysisViewModel = new NewAnalysisViewModel();
        newAnalysisViewModel.NavigationResolver = analysisViewModel.NavigationResolver;

        // set CurrentPosition to -1 so that none is selected.
        analysisViewModel.Measurements.MoveCurrentToPosition(-1);
        analysisViewModel.Measurements.Refresh();
        if(((List<MeasurementViewModel>)(analysisViewModel.Measurements.SourceCollection)).Count == 0)
        {
            CanExecuteChanged(this, new EventArgs());
        }
        analysisViewModel.NavigationResolver.GoToAnalysisSettings(newAnalysisViewModel);

    }

    /// <summary>Notifies command to update CanExecute property.</summary>
    private void updateCanExecuteChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        CanExecuteChanged(this, new EventArgs());
    }
}

我可以做些什么来使用该线程中的那个对象的任何建议都非常有用。

【问题讨论】:

    标签: c# wpf multithreading visual-studio visual-studio-2010


    【解决方案1】:

    崩溃的原因很可能是因为网络事件没有发生在 GUI 线程上。
    CanExecuteChanged 的调用用于修改作为 GUI 对象的按钮。
    但 GUI 对象只能在 GUI 线程上修改。

    快速修复:

    public class NewAnalysisCommand : ICommand
    {
       // ...
       private Dispatcher dispatcher;
       public NewAnalysisCommand()
       {
          // The command captures the dispatcher of the GUI Thread
          dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
       }
       private void updateCanExecuteChanged(object sender, NotifyCollectionChangedEventArgs e)
       {
          // With a little help of the disptacher, let's go back to the gui thread.
          dispatcher.Invoke( () => { 
                 CanExecuteChanged(this, new EventArgs()); } 
          );
       }
    }
    

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      相关资源
      最近更新 更多