我创建了一个 winforms 多线程应用程序。一旦主线程启动,就会在另一个新生成的后台线程上创建一个启动屏幕表单。
Code:
static SplashForm sf = new SplashForm ();
main()
{
...
Thread t = new Thread(new ThreadStart(runSplash));
t.IsBackground = true;
t.start();
Application.Run(new MainForm());
...
}
void runSplash()
{
Application.Run(sf);
}
稍后在主线程上创建一个 MainForm,在 mainform 的加载事件处理程序中关闭启动表单。
有时 UI 挂起。
即它不响应键盘或鼠标操作。但是主线程和其他线程正在优雅地运行。
使用 spy++ 得到以下日志消息
<00001> 000103A6 S WM_WINDOWPOSCHANGING lpwp:003DF0DC
<00002> 000103A6 R WM_WINDOWPOSCHANGING
<00003> 000103A6 S WM_NCPAINT hrgn:00000001
<00004> 000103A6 S WM_GETTEXT cchTextMax:510 lpszText:003DDBAC
<00005> 000103A6 R WM_GETTEXT cchCopied:37 lpszText:003DDBAC ("I")
<00006> 000103A6 R WM_NCPAINT
<00007> 000103A6 S WM_ERASEBKGND hdc:D5010AC2
<00008> 000103A6 R WM_ERASEBKGND fErased:True
<00009> 000103A6 S WM_WINDOWPOSCHANGED lpwp:003DF0DC
<00010> 000103A6 R WM_WINDOWPOSCHANGED
<00011> 000103A6 S WM_ACTIVATEAPP fActive:True dwThreadID:000008A8
<00012> 000103A6 R WM_ACTIVATEAPP
<00013> 000103A6 S WM_NCACTIVATE fActive:True
<00014> 000103A6 R WM_NCACTIVATE
<00015> 000103A6 S WM_ACTIVATE fActive:WA_ACTIVE fMinimized:False hwndPrevious:(null)
<00016> 000103A6 R WM_ACTIVATE
<00017> 000103A6 S WM_IME_SETCONTEXT fSet:1 (LONG)iShow:C000000F
<00018> 000103A6 S WM_IME_NOTIFY dwCommand:00000002 dwData:00000000
<00019> 000103A6 R WM_IME_NOTIFY
<00020> 000103A6 R WM_IME_SETCONTEXT
<00021> 000103A6 S WM_SETFOCUS hwndLoseFocus:(null)
<00022> 000103A6 R WM_SETFOCUS
<00023> 000103A6 P WM_PAINT hdc:00000000
<00024> 000103A6 S WM_SETCURSOR hwnd:000103A6 nHittest:FFFE wMouseMsg:WM_MOUSEMOVE
<00025> 000103A6 R WM_SETCURSOR fHaltProcessing:False
<00026> 000103A6 S WM_SETCURSOR hwnd:000103A6 nHittest:FFFE wMouseMsg:WM_MOUSEMOVE
<00027> 000103A6 R WM_SETCURSOR fHaltProcessing:False
<00028> 000103A6 S WM_NCACTIVATE fActive:False
<00029> 000103A6 S WM_GETTEXT cchTextMax:510 lpszText:003DDBAC
<00030> 000103A6 R WM_GETTEXT cchCopied:37 lpszText:003DDBAC ("I")
<00031> 000103A6 R WM_NCACTIVATE fDeactivateOK:True
<00032> 000103A6 S WM_ACTIVATE fActive:WA_INACTIVE fMinimized:False hwndPrevious:(null)
<00033> 000103A6 R WM_ACTIVATE
<00034> 000103A6 S WM_ACTIVATEAPP fActive:False dwThreadID:00000BB0
<00035> 000103A6 R WM_ACTIVATEAPP
<00036> 000103A6 S WM_KILLFOCUS hwndGetFocus:(null)
<00037> 000103A6 R WM_KILLFOCUS
<00038> 000103A6 S WM_IME_SETCONTEXT fSet:0 (LONG)iShow:C000000F
<00039> 000103A6 S WM_IME_NOTIFY dwCommand:00000001 dwData:00000000
<00040> 000103A6 R WM_IME_NOTIFY
<00041> 000103A6 R WM_IME_SETCONTEXT
<00042> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
<00043> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
<00044> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
<00045> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
<00046> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
<00047> 000103A6 P message:0xC185 [Registered:"WindowsForms12_ThreadCallbackMessage"] wParam:00000000 lParam:00000000
是什么导致了这个问题以及如何摆脱这个问题?