Donc, dans ma méthode XXX.OnPropertyChanged() J'ai:Pourquoi ai-je parfois une exception NullReferenceException dans le bloc BeginInvoke?
public class XXX : IProperyNotifyChanged {
Control itsCtrl;
...
public void Init(Control ctrl) {
itsCtrl = ctrl;
}
public void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null) {
if (itsCtrl.InvokeRequired) {
itsCtrl.BeginInvoke(() => {
PropertyChanged(this, propertyName);
});
} else {
PropertyChanged(this, propertyName);
}
}
}
}
Je pense que cela jette l'exception suivante (rarement, mais arrive plus souvent maintenant):
System.Reflection.TargetInvocationException was unhandled
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at DevExpress.XtraEditors.XtraForm.WndProc(Message& msg)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at DevExpress.Utils.Win.Hook.ControlWndHook.CallWindowProc(IntPtr pPrevProc, IntPtr hWnd, Int32 message, IntPtr wParam, IntPtr lParam)
at DevExpress.Utils.Win.Hook.ControlWndHook.WindowProc(IntPtr hWnd, Int32 message, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr 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 Client.Program.Main() in C:\Client\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=XXX
StackTrace:
at XXX.<>c__DisplayClass442_0.<OnPropertyChanged>b__0()
InnerException:
Je pensais juste. Est-ce que cela se produit parce que je ne copie pas correctement les variables comme this
et propertyName
avant d'appeler BeginInvoke
? Ou s'agit-il d'autre chose? Cela arrive si rarement que je ne suis pas sûr de savoir comment le reproduire et je ne peux pas vraiment obtenir beaucoup de la trace de la pile. Comment voulez-vous résoudre ce problème?
est "itsCtrl" jamais nulle? Ajoutez une vérification null avant de commencer à invoquer et de voir si le problème disparaît – bgura
itsCtrl est le formulaire principal. Cela ne peut jamais être nul. – Denis
@Denis Ce n'est pas vrai. Lorsque le formulaire est en cours de chargement, il peut être 'null'. Vous rencontrez une condition de concurrence. –