.net 继续获得';交叉线程操作无效';尝试显示Notify图标的菜单提示时
这不是一个常规的“Corss线程操作”BS,所以请不要发布明显的解决方案 这件事已经让我发疯好几天了 我有一个.net 继续获得';交叉线程操作无效';尝试显示Notify图标的菜单提示时,.net,multithreading,notifyicon,contextmenustrip,.net,Multithreading,Notifyicon,Contextmenustrip,这不是一个常规的“Corss线程操作”BS,所以请不要发布明显的解决方案 这件事已经让我发疯好几天了 我有一个NotifyIcon,带有一个绑定的ContextMenuStrip。 一切都按它应该的方式工作,但有时(我记不起复制的确切步骤)会出现交叉线程异常。 我对ContextMenuStrip/NotifyIcon的所有访问权限都用Invoke cr*p填充,但当用户右键单击notify图标时,仍会引发异常 这是异常的完整数据: 消息:跨线程操作无效:从创建控件“”的线程以外的线程访问该控件
NotifyIcon
,带有一个绑定的ContextMenuStrip
。
一切都按它应该的方式工作,但有时(我记不起复制的确切步骤)会出现交叉线程异常。
我对ContextMenuStrip
/NotifyIcon
的所有访问权限都用Invoke cr*p填充,但当用户右键单击notify图标时,仍会引发异常
这是异常的完整数据:
消息
:跨线程操作无效:从创建控件“”的线程以外的线程访问该控件“”
调用堆栈:
在System.Windows.Forms.Control.get_Handle()中
在System.Windows.Forms.Control.SetBoundsCore(Int32 x,Int32 y,Int32 width,Int32
高度、边界(指定)
位于System.Windows.Forms.ToolStrip.SetBoundsCore(指定了Int32 x、Int32 y、Int32宽度、Int32高度、边界)
位于System.Windows.Forms.ToolStripDropDown.SetBoundsCore(指定了Int32 x、Int32 y、Int32宽度、Int32高度、边界)
位于System.Windows.Forms.Control.SetBounds(指定了Int32 x、Int32 y、Int32宽度、Int32高度、边界)
在System.Windows.Forms.Control.set_位置(点值)
在System.Windows.Forms.ToolStripDropDown.Show(Int32 x,Int32 y)中
位于System.Windows.Forms.ContextMenuStrip.ShowInTaskbar(Int32 x,Int32 y)
在System.Windows.Forms.NotifyIcon.ShowContextMenu()中
在System.Windows.Forms.NotifyIcon.WndProc(Message&msg)
在System.Windows.Forms.NotifyIcon.NotifyIconNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.PeekMessage(MSG&MSG,HandleRef hwnd,Int32 msgMin,Int32 msgMax,Int32 remove)
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
位于D:\SDMS14SP2\StarLIMS.Grabber\StarLIMS.Grabber.Manager\Program.cs中的StarLIMS.Grabber.Manager.Program.Main(字符串[]args):第34行
位于System.AppDomain.\u nexecutestAssembly(程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
正如道根所指出的,我的
ContextMenuStrip
是在OnLoad下初始化的,而不是在contor下初始化的。。。一旦我把它移到ctor中,问题就消失了。正如道根指出的那样,我的ContextMenuStrip
是在OnLoad下初始化的,而不是ctor。。。一旦我在ctor中移动了它,问题就消失了。请注意,异常消息就是我得到的确切消息,“”内的值为空。是否可能在主线程以外的其他线程上创建了NotifyIcon?能否将您填充的Invoke cr*p访问权发布到ContextMenuStrip/NotifyIcon
?当用户右键单击时会发生什么?我们能看到代码吗?@Doggett:谢谢你的启发-我正在OnLoad下创建NotifyIcon,而不是构造函数-我想这解决了它。非常感谢通知,异常消息正是我收到的消息,“”内的值为空。是否可能在主线程以外的其他线程上创建了NotifyIcon?能否将您填充的Invoke cr*p访问权发布到ContextMenuStrip/NotifyIcon
?当用户右键单击时会发生什么?我们能看到代码吗?@Doggett:谢谢你的启发-我正在OnLoad下创建NotifyIcon,而不是构造函数-我想这解决了它。非常感谢你你忽略了这个问题。真正的问题是OnLoad在错误的线程上运行。这迟早会打你屁股的。设置一个断点。对我来说,听起来像是“常规”Corss线程操作。我不会“发布明显的解决方案”,告诉你如何解决线程问题,但你真的应该这样做。你只是忽略了这个问题。真正的问题是OnLoad在错误的线程上运行。这迟早会打你屁股的。设置一个断点。对我来说,听起来像是“常规”Corss线程操作。我不会“发布明显的解决方案”,告诉您如何着手解决线程问题,但您确实应该这样做。