疑为多线程UI引发的的异常
程序是这样的:
窗体调用了一个dll模块(若干类),这个模块进行异步通讯,快速的收到很多数据,并根据数据生成了一个bitmap的一行,触发事件linefinished。主窗体中订阅这个事件,并需要飞快的更新Picturebox 显示生成的bitmap。
现在的问题是,会出现两种异常。
第一种是,刷新picture时bitmap对象正在使用中。
我尝试用synclock在生成bitmap和现实bitmap的地方进行互斥锁,但是还是偶尔出现对象正在使用的异常。
第二种很致命,在刷新picture时,对窗体进行可能会引发重绘的操作(窗体mousedown、窗体拖动、最大化等)导致对象正在使用中的异常,但不知道是哪个异常。据我怀疑是有2个线程(窗体操作线程和订阅的linefinished事件)同时重绘导致的。
错误信息如下:
未处理 System.InvalidOperationException
Message="对象当前正在其他地方使用。"
Source="System.Drawing"
StackTrace:
在 System.Drawing.Image.get_RawFormat()
在 System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
在 System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
在 System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(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(ApplicationContext context)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 STM_Controller.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
我不知道VB的main函数在哪里,也不知道这个问题如何解决。请教各位大大,这个问题该怎么办?
若不能在子线程所在的dll模块中,调用外部窗体invoke方法的话,这个程序又该如何设计?
我尝试过的方法:
bitmap.clone不能解决异常二;
刷新由linefinished触发,改为窗体Timer_tick事件触发,不能解决异常二;
刷新语句用try Catch包住,依然不能解决异常二。
……
怎么办怎么办????