开发者

Keep getting the 'Cross-thread operation not valid' when trying to show the menustrip of a notifyicon

开发者 https://www.devze.com 2023-01-29 10:14 出处:网络
This is not a regular \'Corss-Thread opeartion\' BS, so don\'t post the obvious solutions please. This is something that\'s been driving me crazy for days now.

This is not a regular 'Corss-Thread opeartion' BS, so don't post the obvious solutions please.

This is something that's been driving me crazy for days now.

I have a NotifyIcon, with a binded ContextMenuStrip. Everything works as it should, but sometimes (and I can't recall the exact steps to reproduce this) I get the cross-thread exception. All my access to the ContextMenuStrip/NotifyIcon is padded with the Invoke cr*p, but still - when the user right-clicks on the notify icon - the exception is raised.

This is the full data from the exception:

Message: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

Call stack:

at System.Windows.Forms.Control.get_Handle()

at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)

at System.Windows.Forms.ToolStrip.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)

at System.Windows.Forms.ToolStripDropDown.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)

at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)

at System.Windows.Forms.Control.set_Location(Point value)

at System.Windows.Forms.ToolStripDropDown.Show(Int32 x, Int32 y)

at System.Windows.Forms.ContextMenuStrip.ShowInTaskbar(Int32 x, Int32 y)

at System.Windows.Forms.NotifyIcon.ShowContextMenu()

at System.Windows.Forms.NotifyIcon.WndProc(Message& msg)

at System.Windows.Forms.NotifyIcon.NotifyIconNativeWindow.WndProc(Message& m)

at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)

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

at S开发者_开发知识库ystem.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 StarLIMS.Grabber.Manager.Program.Main(String[] args) in D:\SDMS14SP2\StarLIMS.Grabber\StarLIMS.Grabber.Manager\Program.cs:line 34

at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()


As Doggett pointed - my ContextMenuStrip was initialized under the OnLoad, instead of the ctor... once I moved it in the ctor - problem disappeared.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号