Blog Archives

C# Application Not Responding Issue on Long Running Threads

Undoubtedly many of you have encountered the dreaded “Application Not Responding” message, programmers and non-programmers alike.

In general, this is caused by the OS misinterpretation of long delays in form refresh, even though these can easily be caused by a long running process, or any running process longer than a couple seconds actually.

If this is happening in your application after clicking on a button or event fires, a quick solution would be the easy one-liner below:


However, to really understand why this happening, remember that all processes (may) contain multiple threads.

In the case of your typical quick shot solution that you put together in VS in no-time, you’re likely only using a single thread.

This thread is used not only to run the processes in your application, but also to update the form window including drawing the fields and various controls on the form.

The best solution to this would really be to create a separate thread and pass off the work of your long running function to this thread.

When the work is finished, it will update our main thread (the form thread) that it has completed which we can intercept with a delegate, and notify the user of success or failure.

See snippet below.

private BackgroundWorker hardworker; //this guys works hard!
private System.Timers.Timer lazyworker; //notice how this could also be achieved with thread/timer

protected void btnDoAlotOfWork_Click() {
//lets see who accomplishes more!

private void ClockInLazyWorker() {
lazyworker = new System.Timers.Timer(); //not to be confused with forms timer
lazyworker.Interval = 1000; //time in milliseconds. 10 seconds is lazy for a computer!
            lazyworker.Elapsed += new System.Timers.ElapsedEventHandler(LetsGetAMoveOnIt());

private void ClockInHardWorker() {
hardworker= new BackgroundWorker();
            mCopier.DoWork += DoWork; //all the heavy lifting is done here
            mCopier.RunWorkerCompleted += WorkCompleted();
            mCopier.WorkerSupportsCancellation = true;
            OnError += ProblemWithWork();

//motivational function name made especially for our lazy worker
private void LetsGetAMoveOnIt() {
//do something that takes a long time
//if the data you operate on is the same as used by another thread or the form thread, make sure you wrap this block in a synclock or use a bool to track when work is in process

//created separate function for the background worker, and also to avoid synclock issues since there are two new active threads
private void DoWork() {
//if the data you operate on is the same as used by another thread or the form thread, make sure you wrap this block in a synclock or use a bool to track when work is in process. same goes for timer.

private void WorkCompleted() {
//all finished

private void ProblemWithWork() {
//hopefully not an injury on the job! that's workmans comp!

Alot more code than simply “Application.DoEvents”.. but do not underestimate the power of multiple threads!


AppActivate In C#

AppActivate, which many of you may recognize from VB activates a window for an application which is already running, bringing the window of that app to the foreground.

In C#, there is no exact equivalent to this function, which leaves two alternatives (see link at the bottom for code examples):

Option 1 (recommended):
Use SetForeGroundWindow, described in my similar post (scroll down a bit for the code examples).

Option 2:
Add a reference to Microsoft.VisualBasic and call Interactive.AppActivate.

Although Option 2 is a little simpler/faster if you are not familiar with or ready yet to delve into Win32 API, it is not a recommended solution as there can be potential side effects of this approach.

For code examples of both options, see my similarly titled post below and scroll through for code references.

Start a Process in the Foreground in C# .Net Without AppActivate

Get Selected Item from ListBox in Winforms C# and VB .Net

Iterate through all items:

  foreach(ListItem item in ListBox1.Items)
       if (item.Selected == True)
          //item.Value //no to be confused with text
          //item.Text //text displayed on UI

Find specific item text:

  private string GetSelectedItem(ListBox container)
            //string selecteditem = "";
            for (int i = 0; i < container.Items.Count; i++)
                if (container.Items[i].Selected == true)
                    //use .value alternatively if you are trying to get value instead of text
                    return ListBox1.Items[i].Text; //breaks the loop or
                    //break; //if you decide to use without a function
            return ""; //error handled in calling func, remove return value if used without a function
            //return selecteditem ; //if you want to iterate all items anyway

More generalized for re-usability to get text or value, though a little less readable and alot more code:

private static class GetSelectedItem {
private enum ListBox_ContentType {

public string Text(ListBox container) { Get_ContentType(container,ListBox_ContentType.Text);}
public string Value(ListBox container) { Get_ContentType(container,ListBox_ContentType.Value);}

private string Get_ContentType(ListBox container, ListBox_ContentType ContentType) {
  for (int i = 0; i < container.Items.Count; i++)
                if (container.Items[i].Selected == true)
if (ContentType == ListBox_ContentType.Value) {
return ListBox1.Items[i].Text;
else //ContentType == ListBox_ContentType.Value 
                    return ListBox1.Items[i].Value;
            return "";

Change WPF Toolbar and Window Skin

Took me some Googling to come across some good links to help me find exactly what I was looking to do. Hopefully my post gets enough hits that you’ll come across my title sooner rather than later, and can benefit from my time and research. 🙂

If you’re new to WPF, or even if you’ve been using WPF for some quite some time, you may or may not have noticed that theming and skinning is not relatively straight forward, and there are many different ways to achieve the end result visually.

If you’re using WPF simply to learn the new technology, understand that there are pro’s and con’s to WPF vs standard Winforms, and although the latter is technically “legacy”, depending on third party controls you may or may not be using, implementation in Winforms can be very fast and may still be a better option for some projects.

Here’s a simple grid I’ve come up with to help you decide which to use:

  WinForms WPF
Supports Skins No Yes
Uses graphics resources to render No Yes
Easily bind controls with data No Yes
Easy to design a form with drag+drop Yes No
Some cross-platform compatibility with Mono Yes No
Easily customize look and feel of controls No Yes
Already comes with most controls/visuals needed for business app Yes No

As you can see from the table above, if you are trying to quickly knock out a business app that is not going to require heavy visual modification or too reliant on databinding, it’s probably faster in WinForms. Also, I have personally deployed WinForms cross-platform (Mac, Linux) using mono, which I could not get to work with WPF.

Overall though, for most environments, WPF is usually the way to go if not for visual flexibility then for databinding (why are you using windows client apps again?:P).

One ways listed above that WPF really comes out on top is in utilizing the computer’s graphics card and other graphics resources to render forms. The WPF objects on the screen are still contained in a form however, and many theming examples override the draw methods on the forms themselves which don’t properly utilize the effectiveness of WPF.

In general, I have found there are at least three different ways to change the look and feel of your application in WPF:

Using Silverlight Themes
Using Office Themes
Using your own Custom Themes

If you download and install the Silverlight toolkit (not to be confused with silverlight SDK or silverlight client), you will automatically get multiple dll references which contain XAML themes that can be re-used in WPF. This is a quick way to make your WPF app look good without too much work.

Office themes are pre-built in, and a common choice for many beginner WPF skinners. They share common visuals which you see in many office apps and give your application a more up-to date look and feel with some recognizability by your users also, similar to how Mac applications all share a common theme.

This is also always an option, but be prepared for a long and ardous task and a few lunch days with your favorite designer. Microsoft expressions studio comes with all the tools needed to make your controls as beautiful as you like, and implementing and inheriting from these visuals is actually relatively simple, but when you apply the time it takes to make one control by the amount of different controls you may have, there is alot of work involved. Also for re-usability, you’ll want to put all your custom controls/skins in their own assembly which increases the time even further. If you’re short on time (who isn’t :P), this is not a good option, but in the long-run this could be very beneficial and give your applications their own gleam.

See references for links on how to implement the above and other information regarding skinning and theming in WPF. (The reference links are in order of my google “stream of consciousness” :P)

WPF Skins (this blog), <a href="
MSDN Blog,
Codeproject Article,
Stackoverflow Blog,
Codeplex Article,
WPF Tutorial.Net,
Silverlight.Net Blog,
MSDN Social,
C-sharp Corner Article,
Browsoft Article,
Jan-Cornelius Molnar (Blog),
MSDN Official Blog,

Bind Control to an Object in Windows Forms

Simple solution for basic binding in Windows Forms app. This would NOT be recommended if you are using ASP .Net, Silverlight, WCF, RIA or any other services to retrieve the data as these project types have much better support for binding to controls.


static class dataaccess {
static mytype object;

//on app load
object = new mytype();

//on form load
tbField1.DataBindings.Add("Text",, "Field1")