从两个不同的线程创建lync控件时,为什么会抛出TargetInvocationException [英] When creating lync controls from two different threads, why does it throw a TargetInvocationException

查看:51
本文介绍了从两个不同的线程创建lync控件时,为什么会抛出TargetInvocationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在多个线程中创建窗口的应用程序,它在大多数情况下按预期工作,但是使用Lync客户端控件(特别是ContactSearchInputBox)我现在得到一个TargetInvocationException。 代码有点复杂到
显示在这里,但基本上我正在做的是: 

I have an application that creates windows in multiple threads, which works as expected most of the time, but with the Lync client controls (specifically ContactSearchInputBox) I'm now getting a TargetInvocationException.  Code is a little complex to show here, but essentially what I'm doing is something like: 

线程t =新线程(新的ThreadStart(ShowWindow));

Thread t=new Thread(new ThreadStart(ShowWindow));

ShowWindow()

ShowWindow()

 窗口w =新窗口();

  Window w=new Window();

  w.ShowDialog();

  w.ShowDialog();

 

我第一次显示它时控件工作得很好但是我会关闭孩子窗口并尝试再次打开它,此时我会抓住这个例外。 我知道这些东西不是很多,但有没有人知道这里有什么
? 我使用这种方式的其他控件没有一个具有相同的问题 - 看起来内部的东西不应该使用Dispatcher应该是什么? 

The control works just fine the first time I show it, but then I'll close the child window and try to open it again, at which point I'll catch this exception.  I know that's not a huge amount of stuff to go on, but does anyone have any idea what's going on here?  None of the other controls I'm using this way have the same problem-it looks like something internally isn't using a Dispatcher when it should be? 

 

异常详细信息:

 

System.Reflection.TargetInvocationException:目标引发了异常调用。 ---> System.Windows.Markup.XamlParseException:'System.Windows.Data.BindingExpression'值无法分配给对象'Microsoft.Lync.Controls.Internal.WatermarkedTextBox'的属性'Watermark'。
调用线程无法访问此对象,因为另一个线程拥有它。 对象'System.Windows.Data.Binding'出错。 ---> System.InvalidOperationException:调用线程无法访问此对象,因为另一个线程拥有它。

  在System.Windows.Threading.Dispatcher.VerifyAccess()

  在System.Windows.Threading.DispatcherObject.VerifyAccess()

  在System.Windows.DependencyObject.GetValue(DependencyProperty dp)

   at Microsoft.Lync.Controls.Internal.Converters.BooleanToSelectedObject.Convert(Object value,Type targetType,Object parameter,CultureInfo culture)

  在System.Windows.Data.BindingExpression.TransferValue(Object newValue,Boolean isASubPropertyChange)

  在System.Windows.Data.BindingExpression.Activate(对象项目)

   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)

  在System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target,DependencyProperty dp)

  在System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target,DependencyProperty dp)

  在System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d,DependencyProperty dp)

  在System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField,DependencyObject容器,FrameworkElement feChild,FrameworkContentElement fceChild,Int32 childIndex,DependencyProperty dp,Int32 i,EffectiveValueEntry& entry)

  在System.Windows.TemplateApplicationHelper.SetDependencyValueCore(DependencyObject dependencyObject,DependencyProperty dp,Object value)

  在System.Windows.Markup.BamlRecordReader.SetDependencyValue(DependencyObject dependencyObject,DependencyProperty dependencyProperty,Object value)

  在System.Windows.Markup.BamlRecordReader.SetDependencyComplexProperty(Object currentTarget,BamlAttributeInfoRecord attribInfo,Object o)

   ---内部异常堆栈跟踪结束---

  在Microsoft.Lync.Controls.UCBase。<> c__DisplayClass1。< ThrowFromDispatcher> b__0()

   ---内部异常堆栈跟踪结束---

   at System.RuntimeMethodHandle._InvokeMethodFast(Object target,Object [] arguments,SignatureStruct& sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)

   at System.RuntimeMethodHandle.InvokeMethodFast(Object target,Object [] arguments,Signature sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)

   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object [] parameters,CultureInfo culture,Boolean skipVisibilityChecks)

  在System.Delegate.DynamicInvokeImpl(Object [] args)

   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Boolean isSingleParameter)

   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)

  在System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,Object args,Boolean isSingleParameter,Delegate catchHandler)

  在System.Windows.Threading.DispatcherOperation.InvokeImpl()

  在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)

   at System.Threading.ExecutionContext.runTryCode(Object userData)

  在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,Object userData)

  在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态)

  在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)

  在System.Windows.Threading.DispatcherOperation.Invoke()

  在System.Windows.Threading.Dispatcher.ProcessQueue()

  在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handling)

  在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& processed)

  在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)

   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Boolean isSingleParameter)

   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)

  在System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,Object args,Boolean isSingleParameter,Delegate catchHandler)

   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter)

   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate method,Object arg)

  在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)

  在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

  在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)

  在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)

  在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)

  在System.Windows.Forms.Application.Run(Form mainForm)

  在iceOfficeAgentCOMService.BaseForm.RunThread()

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Windows.Markup.XamlParseException: 'System.Windows.Data.BindingExpression' value cannot be assigned to property 'Watermark' of object 'Microsoft.Lync.Controls.Internal.WatermarkedTextBox'. The calling thread cannot access this object because a different thread owns it.  Error at object 'System.Windows.Data.Binding'. ---> System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.Threading.DispatcherObject.VerifyAccess()
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at Microsoft.Lync.Controls.Internal.Converters.BooleanToSelectedObject.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
   at System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp)
   at System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp)
   at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
   at System.Windows.TemplateApplicationHelper.SetDependencyValueCore(DependencyObject dependencyObject, DependencyProperty dp, Object value)
   at System.Windows.Markup.BamlRecordReader.SetDependencyValue(DependencyObject dependencyObject, DependencyProperty dependencyProperty, Object value)
   at System.Windows.Markup.BamlRecordReader.SetDependencyComplexProperty(Object currentTarget, BamlAttributeInfoRecord attribInfo, Object o)
   --- End of inner exception stack trace ---
   at Microsoft.Lync.Controls.UCBase.<>c__DisplayClass1.<ThrowFromDispatcher>b__0()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at iceOfficeAgentCOMService.BaseForm.RunThread()

推荐答案

您好Chris应该定义使用Dispatcher,因为您从工作人员访问GUI线。尝试类似:

Hi Chris, you should definetely use a Dispatcher, since you access the GUI from a worker thread. Try something like:

// GUI的更新必须在主线程上发生

// updates of GUI must happen on the main thread

Dispatcher.CurrentDispatcher.Invoke(new Action(()=> ;

Dispatcher.CurrentDispatcher.Invoke(new Action(() =>

{

...

}));

 

我希望它有所帮助,

Ratko。


这篇关于从两个不同的线程创建lync控件时,为什么会抛出TargetInvocationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆