WPF 错误:调用线程无法访问此对象,因为其他线程拥有它

问题描述

我知道有很多关于这个错误的帖子,但他们的答案对我没有帮助。

我的问题是我在 WPF 应用程序中使用了不同的类。此外,我对 UI 的重绘是在一个自己的类中(见下面的代码)。

也许有人可以帮助我。

代码示例:

那是主窗口

namespace Project.View
{
  public partial class MainWindow : Window
  {
    public static MainWindow AppWindow;
    public static Thread MainWindowThread;
    private IShare iShare;
    public bool LoggedIn;
    public MainWindow()
    {
      InitializeComponent();
      MainWindowThread = Thread.CurrentThread;
      AppWindow = this;
      LoggedIn = false;
    }
    private void Login_Click(object sender,RoutedEventArgs e)
    {
      if(LoggedIn == true)
      {
        RedrawFunctions.logout_default();
        LoggedIn = false;
      }
      else
      {
        iShare = new IShare(Username.Text,Password.Password,"...");
        iShare.StartConnection();
      }
    }
  }
}

那是 IShareLogin

namespace Project.Threads
{
  public class IShare
  {
    private static readonly object IShareLock = new object();
    private static IShareAccess IShare;
    private Thread Connect;
    private readonly string Username;
    private readonly string Password;
    private readonly string Domain;
    public event EventHandler StopErrorInfo;
    
    public IShareLogin(string IShareUsername,string ISharePassword,string IShareDomain)
    {
      Username = IShareUsername;
      Password = ISharePassword;
      Domain = IShareDomain;
    }
    public IShareLogin(){}
    public void StartConnection()
    {
      StopErrorinformation(EventArgs.Empty);
      Connect = new Thread(() => IShareConnect())
      {
        Name = "IShareLogin"
      };
      Connect.Start();
    }
    public void StopConnection()
    {
      StopErrorinformation(EventArgs,Empty);
      IShare = null;
    }
    private static void IShareConnect()
    {
      lock(IShareLock)
      {
        try
        {
          RedrawFunctions.Logininformation_Content("Try to connect");
          RedrawFunctions.Logininformation_BackgroundColor(Brushed.LightCyan);
          RedrawFunctions.Logininformation_Visible(Visibility.Visible);
          IShare = new IShareAccess(Username,Password,Domain);
          RedrawFunctions.Login_default();
          MainWindow.AppWindow.LoggedIn = true;
          IShareLoginFailed.StopPreviews = false;
        }catch
        {
          IShareLoginFailed.LoginDataFalse();
        }
      }
    }
    protected virtual void StopErrorinformation(EventArgs e)
    {
      StopErrorInfo?.Invoke(this,e);
    }
  }
}

即 IShareLogin 失败

namespace Project.Threads
{
  public static class IShareLoginFailed
  {
    private static readonly Stopwatch ShowAlertTime = new Stopwatch();
    private static readonly IShareLogin EventIShare = new IShareLogin();
    private static bool WindowWollBeClosedSoon = false;
    private static bool TryLogin = false;
    private static SolidColorBrush Fill;
    private static readonly object LoginLock = new object();
    public static bool StopPreviews;
    
    public static void LoginDataFalse()
    {
      lock(LoginLock)
      {
        EventIShare.StopErrorInfo += new EventHandler(CancelToken);
        MainWindow.AppWindow.Closing += new 
        System.ComponentModel.CancelEventHandler(WindowCloses);
        MainWindow.AppWindow.Login_Button.Click += new RoutedEventHandler(LoginIsClicked);
        RedrawFunctions.Logininformation_Content("Username or Password is wrong");
        Fill = new SolidColorBrush(Color.FromArgb(255,255,186,186));
        RedrawFunctions.Logininformation_BackgroundColor(Fill);
        RedrawFunctions.Logininformation_Visible(Visibility.Visible);
        ShowAlertTime.Start();
        while(WindowWillBeClosedSoon == false && ShowAlertTime.ElapsedMilliseconds <= 5000)
        {
          if(TryLogin == true || StopPreviews == true)
          {
            break;
          }
          Thread.Sleep(50);
        }
        ShowAlertTime.Stop();
        ShowAlertTime.Reset();
        RedrawFunctions.Logininformation_Visible(Visibility.Hidden);
        RedrawFunctions.Logininformation_BackgroundColor(Brushes.Transparent);
        RedrawFunctions.Logininformation_Content("");
        EventIShare.StopErrorInfo -= CancelToken;
        MainWindow.AppWindow.Closing -= WindowCloses;
        MainWindow.AppWindow.Login_Button.Click -= LoginIsClicked;
        TryLogin = false;
        RedrawFunctions.logout_default();
      }
    }
    private static void WindowCloses(object sender,System.ComponentModel.CancelEventArgs e)
    {
      WindowWillBeClosedSoon = true;
    }
    private static void LoginIsClicked(object sender,RoutedEventArgs e)
    {
      TryLogin = true;
    }
    private static void CancelToken(object sender,EventArgs e)
    {
      StopPreviews = true;
    }
  }
}

那是 RadrawClass

namespace Project.View
{
  public partial class RedrawFunctions : MainWindow
  {
    public static void Logininformation_Content(string name)
    {
      AppWindow.dispatcher.Invoke(() => AppWindow.Logininformation.Content = name);
    }
    public static void Logininformation_Visible(Visibility Visible)
    {
      AppWindow.dispatcher.Invoke(() => AppWindow.Logininformation.Visibility = Visible);
    }
    public static void Logininformation_BackgroundColor(Brush brushColor)
    {
      AppWindow.dispatcher.Invoke(() => AppWindow.Logininformation.Background = brushColor);
    }
  }
}

也许有人知道,如何清除此错误。它总是在程序调用 RedrawFunctions.Logininformation_Visible 函数时发生。

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)