0

I have created a winforms multithreaded application. As soon as main thread starts, a form for splash screen is created on another newly spawned background thread.

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);
}

Later a MainForm is created on the main thread, splash form is being closed in the load event handler of mainform.

Sometimes UI hangs.

i.e. it doesn't respond to keyboard or mouse operations. But the mainthread and other threads are running gracefully.

Got the following log messages using 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

What is causing this issue and how to get rid of this?

user186246
  • 1,857
  • 9
  • 29
  • 45
  • 1
    It's impossible to say anything about the problem without further information. e.g. when exactly does it hang? What code is executed at that moment? – Daniel Hilgarth Aug 17 '12 at 05:49
  • Can you attach to the application when it is in hung state and inspect the threads in Threads Window in Visual Studio. This will point you in the right direction about which thread is waiting on which object/thread. – Ganesh R. Aug 17 '12 at 05:52

1 Answers1

3

Why spy++? You need to create a memory dump (using winDBG or etc) and analyze it.

Sometimes UI hangs i.e. it doesn't respond to keyboard or mouse operations. But the mainthread and other threads are running gracefully.

The GUI needs to be on the main thread. This code shows you have the splash screen in another thread:

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

Windows Forms controls are not inherently thread safe, you can read up everywhere about this, eg:
In WinForms, why can't you update UI controls from other threads?

Put all your UI stuff back in the main thread and let us know if the problem persists.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321