调用线程无法访问此对象

调用线程无法访问此对象

本文介绍了c#WPF错误调用线程无法访问此对象,因为另一个线程拥有它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! /// < 摘要 > /// function put username and password /// < / summary > /// < param name = sender > < / param > /// < param 名称 = e > < / param > private void m_vpn_needLoginAndPassword(object sender,NeedLoginAndPasswordEventArgs e) { e.UserName = username.Text; e.Password = password.Password; } 错误 调用线程无法访问此对象,因为另一个线程拥有它。 i use if (Application.Current.Dispatcher.CheckAccess()) { e.UserName = username.Text; e.Password = password.Password; } 其他 { // 其他明智的方法是用UI线程访问重新调用方法 e.UserName = username.Text; e.Password = password.Password; } < / pre > 不工作 谢谢解决方案 一切正确,正如异常消息告诉您的那样。在 else 分支上,使用 Dispatcher.Invoke : http://msdn.microsoft.com/en-us/library/system.windows.threading。 dispatcher.invoke.aspx [ ^ ]。 如需解释,请查看我过去的答案: Control.Invoke()与Control.BeginInvoke() [ ^ ], Treeview扫描仪和MD5的问题 [ ^ ]。 -SA /// <summary>/// function put username and password/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void m_vpn_needLoginAndPassword(object sender, NeedLoginAndPasswordEventArgs e){ e.UserName = username.Text; e.Password = password.Password;}errorThe calling thread cannot access this object because a different thread owns it.i useif (Application.Current.Dispatcher.CheckAccess()){ e.UserName = username.Text; e.Password = password.Password;}else{ //Other wise re-invoke the method with UI thread access e.UserName = username.Text; e.Password = password.Password;}</pre>not workthank you 解决方案 All correct, exactly as the exception message tells you. On the else branch, use Dispatcher.Invoke:http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx[^].For some explanations, please see my past answers:Control.Invoke() vs. Control.BeginInvoke()[^],Problem with Treeview Scanner And MD5[^].—SA 这篇关于c#WPF错误调用线程无法访问此对象,因为另一个线程拥有它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 08:38