19 Threads for WPF Application - wpf

I just created a new WPF application which has about 5 windows and makes use of the Xceed Data Grid. After looking in the task manager I saw that this small application had 19 threads running.
Can this be correct? I expected the WPF framework to use 2-3 threads, but not 19.
Or am I doing anything wrong?

First, it isn't 19.
When running a "Hello, World!" WPF app, I see 10 threads in Task Manager. When running the same app using the Visual Studio debugger, it uses 19. So, first, factor out 9 of these added as overhead from the debugger.
Now for the 10.
According to a MSDN Magazine article called "Build More Responsive Apps With The Dispatcher", WPF starts out with two threads:
All WPF applications start out with
two important threads, one for rendering and one for managing the user interface. The rendering thread
is a hidden thread that runs in the
background, so the only thread that
you ordinarily deal with is the UI
thread.
But according to this article, there are lots of basic threads in all managed apps, WPF or not:
There are different types of .NET
threads: finalizer, GC, debugger,
timer, threadpool, primary and
background threads. But not all
threads related to .NET are managed.
Starting with the two from WPF, add in the GC and the finalizer to get up to four. Add in the debugger thread (all managed apps have one even when not debugging) to get to five. Add the timer to get to six.
And, attaching a debugger to my WPF app after starting it outside VS, I can see there are two worker threads in the thread pool. This is likely a minimum. This gets my app up to eight.
There are two I cannot account for exactly, but they are likely COM threads for interop (they are definitely not managed threads). The second article I referenced discusses more of these.
But the real moral of the story here is, don't worry about them. If a vanilla "Hello, World!" app uses 10, that is your baseline. Only worry about threads you create beyond this minimum.

Related

Adobe Reader embedded in WinForms app hangs on close

(This appears to be caused by a recent update to Adobe Reader.)
We have a WinForms app that embeds an Adobe Reader through a WebBrowser control (i.e., not by referencing the ActiveX DLL directly). When closing the last instance of that control, the entire app hangs for about 15 seconds.
I can "work around" this by not disposing the control; see also this related answer. But that hardly sounds like a safe solution.
During the hang,
nothing of note happens in our user-level code, according to dotTrace
nothing at all happens in the AcroRd32 process, according to ProcMon. Literally zilch. There's lots of activity before the hang for a second or two, then nothing, then it starts again another almost exactly 15 seconds later.
I'm guessing this is some kind of lock with a timeout, but I don't really know how to track this down any further.

Parent window not receiving window's messages (Key Events)

I have a GUI application that is written using win API's
and we need to launch a new GUI application when the user selects some command menu items.
We decided to write the new application in PyQt and launch the PyQt application usig Python C Api.
Everything is working fine except that the Parent window, through which we launch the PyQt Application, is not responding to some of the events when PyQt application is open. Once we close the PyQt Application it starts responding again to the key events.
I guess, that once the PyQt Gui application is launched, somehow the messages are not passed to the Parent window.
Inspecting with Spy++ I've found the following result:
Receives messages for:
- ALT key
- F1, F2 keys
- Mouse events
Does NOT receive messages for:
- CTRL key
- All other Fn keys
- All letter keys
- SHIFT, CAPS keys
Any thoughts to solve this problem would be appreciated
I believe what you are trying to do -- operate two separate GUIs within a single process -- is not supported by any major operating system. A while back, I searched for a long time for ways to do this and never came up with any advice except "don't".
I'm surprised that missing keys are the only problem you have.. I recommend finding a different solution before you discover more trouble (unless you can find some good evidence that this is at least supposed to work).
Could you perhaps spawn a new process to run the Qt event loop instead? Since you already have python embedded in the main process, this should be fairly easy--use python's built-in IPC to handle the communication between processes.
One solution is to build the QtWinMigrate module to create a QWinHost which supports parenting to a native HWND but unfortunately it is not part of the PyQt distribution.
You can find some sources here: https://github.com/glennra/PyQtWinMigrate.
This is what had to be done for Python integration in 3ds Max by Blur studio. I am currently studying the C++ source code of QWinWidget too see if I can work out an alternative solution using Win32 calls.

Avoiding all system messages and messages from other software

Here is the situation. The company I work for builds this piece of software in c that can make a Windows computer act a bit like a TV. Essentially, our piece of software is meant to be played full screen and content is displayed from the internet without the user having to ever touch the computer again.
The problem is that once in a while, the system brings up pop-ups like "Your Windows system is ready for an upgrade." or "Please renew your Norton subscription" etc. which the user has to periodically and manually remove.
Is there a way to display content full screen without being bothered by those warnings?
Yah, whether or not the development community agrees, Microsoft has several standards for when and why it might be acceptable to have exclusive use of the monitor.
The most official strategy is to use DirectX in exclusive mode. This is what games do, what windows media player does in full screen video with hardware acceleration enabled, etc... If your application is multimedia intensive (as suggested by TV like functionality), you should probably be using DirectX too. Besides giving you the exclusive display access it will also increase your applications performance while lowering the CPU load (as it will overload graphics work to the video card when possible).
If DirectX is not an option, there are a great number of hacks available that seem to all behave differently between various generations of windows operating systems. So you might have to be prepared to implement several techniques to cover each OS you plan to support.
One technique is to set your application as the currently running screensaver. A screensaver if really just an EXE renamed to SCR with certain command line switches it should support. But you can write your own application to be such a screensaver and a little launcher stub that sets it as the screensaver and launches it. Upon exit the application should return the original screensaver settings (perhaps the launcher waits for the process to exit so that it returns the settings in both graceful exits and any unplanned process terminations ie: app crash). I'm not sure if this behavior is consistent across platforms though, you'll have to test it.
Preventing other applications from creating window handles is truly a hack in my opinion and pretty bad one that I wouldn't appreciate as a customer of such software.
A constant BringWindowToTop() call to keep you in front is better (it doesn't break other software) but still a little hack-ish.
Catch window creation messages with a global hook. This way you can close or hide unwanted windows before they become visible.
EDIT: If you definitely want to avoid hooks, then you can call a function periodically, which puts your window to the top of the z-stack.
You could disable system updates http://support.microsoft.com/kb/901037 and remove the norton malware.
You could also connect a second screen so that the bubbles appear in the the first monitor.
Or you rewrite it for linux or windows ce.
One final option is to install software that reconfigures your os into a kiosk http://shop.inteset.com/Products/9-securelockdown.aspx
If you don't need keyboard or mouse input, how about running your application as a screensaver?
A lot of thoses messages are trigged/managed by Windows Explorer.
Just replace it with your dummy c#/winform.
By changing the registry value
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"Shell"="Explorer.exe"
You can specify virtually any exe as an alternative to explorer.exe
That's the way all windows based (embedded) system (ATM & co) do.
There's still few adjustment (disable services you dont need / dr watson & others), and of course, you'll want to keep a "restart explorer.exe" backdoor.
But that's a good start

How to debug and detect hang issue

I am testing my application (Windows 7, WinForms, Infragistics controls, C#, .Net 3.5).
I have two monitors and my application saves and restores forms' position on the first or second monitors. So I physically switched off second monitor and disabled it at Screen Resolution on the windows display settings form. I need to know it is possible for my application to restore windows positions (for those windows that were saved on the second monitor) to the first one.
I switched off second monitor and press Detect to apply hardware changes.
Then Windows switched OFF the first monitor for a few seconds to apply new settings. When the first monitor screen came back, my application became unresponsive. My application was launched in debug mode, so I tried to navigate via stack and threads (Visual Studio 2008), paused application, started and did not find any thing that help me to understand why my application is not responsive. Could somebody help my how to detect the source of issue.
Download the Debugging Tools For Windows then run adplus in hang mode. The with the resulting .dmp file open in windbg and invoke:
!analyze -v -hang
You should provide more information about what you saw in the call stack and threads windows. Which threads were running? Did you see anything that was waiting in a sleep or join? Have you tried naming your threads so that it's clearer as to what's going on when you pause (though not necessary since you can get this info from the call stack anyway... it's a convenience thing).

WPF application visiblity afte remote desktop

i have a WPF application that sits on top of all other windows. 99% of the time it works perfectly however if i dial into my computer from another location using remote desktop and then close the connection and go back to the original computer, my application is not visible. checking the running processes i can see that it is still running however its not visible at all. I assume it has something to do with the remoting but i was wondering if there is some code i can run say every 20 seconds that sits on its own thread that checks whether it is visible and if not will execute some code whether by pInvoke or .NET to become visible again. Any ideas?
Make sure to upgrade to .NET 3.5sp1. There were some changes with remoting which lead to WPF applications being sent as bitmaps in remote desktop in all situations.
In earlier versions of the framework, there were problems with WPF applications causing very odd behavior when combined with remote desktop.
Another idea - make sure you're using the latest and greatest versions of your graphics drivers from your graphics card vendor. WPF makes heavy use of DirectX, and this could potentially be a driver bug preventing the context from resetting appropriately.

Resources