0

我创建了一个 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

是什么导致了这个问题以及如何摆脱这个问题?

4

1 回答 1

3

为什么是间谍++?您需要创建一个内存转储(使用 winDBG 等)并对其进行分析。

有时 UI 挂起,即它不响应键盘或鼠标操作。但是主线程和其他线程正在优雅地运行。

GUI 需要在主线程上。此代码显示您在另一个线程中有启动画面:

Thread t = new Thread(new ThreadStart(runSplash)); 
t.IsBackground = true; 
t.start();

Windows 窗体控件本质上不是线程安全的,您可以到处阅读有关此内容的信息,例如:
在 WinForms 中,为什么不能从其他线程更新 UI 控件?

将所有 UI 内容放回主线程,如果问题仍然存在,请告诉我们。

于 2012-08-17T05:55:26.047 回答