Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


WinForms multithreading

WinForms multithreading

WinForms multithreading


Need some help on how to setup multi threading in a simple winforms application.

I have a simple WinForms application with a listbox. The app allows the user to open a text file, when the file is successfully opened its added to the list box.

I need to add some code that will fire an event when a filename in the listbox is double-clicked. The code should create a new thread and display a fixed size form which will display the file details e.g. file size, modified date, created etc.

I am able to do the above without threading, I have a generateForm function that programmatically creates a new form and adds in some label controls. The controls show the results of a small function that uses the System.IO namespace to interrogate the file attributes.

I need to add a timer control on the dynamically created forms which will auto update the file attributes every few seconds (to monitor the file attributes).

However when I try to have more than one form generated the app stutters as its waiting on each form to complete its event.  To solve this I would like to generate each form with a timer however have each form contained in a thread. When the dynamic form is closed the thread is destroyed.

There is no interaction between the threaded forms and the main application.  I don't have any experience with threaded applications so I am a bit stumped at the moment as to which approach to take.

Any help would be greatly appreciated.


RE: WinForms multithreading

You should keep all your GUI items on the main thread. This will save you some serious headaches.

Display your form using .Show() and make a second thread start the loading process. Once the thread finishes it should invoke the results back onto the main thread in order to do the GUI update.

This example actually does the invoking on itself when called from a separate thread


RE: WinForms multithreading


You should keep all your GUI items on the main thread. This will save you some serious headaches.

Under VS.NET 2005 you must only update the UI from the main thread, otherwise the runtime will throw an exception (the compiler can't find these problems -- they only show up at runtime).

Note that this also applies when you're using timers -- Timers use threads from the system threadpool, so you cannot update any UI elements from a timer callback, either.

To safely update a UI from another thread, first call IsInvokeRequired().  If this returns true, then you must call BeginInvoke() or Invoke() in order to get back onto the thread that created the form.

Chip H.

If you want to get the best response to a question, please read FAQ222-2244: How to get the best answers first

RE: WinForms multithreading

There is also a BackgroundWorker control you can place on a form that allows you to run a process in the background on a different thread from the user interface.  The DoWork method is where you would run your code for the background thread.  This is new for Visual Studio 2005.

RE: WinForms multithreading

Thanks for the info.

I am using Visual Studio 2003, i.e. .NET 1.1.  I would use Visual Stdio 2005 however I am not able to do this for the current project so thats out of the question (BackgroundWorker has its own limitations as well).

As I stated, there is no interaction between the thread and the main form (except for when the thread is killed, it should handle that event to make sure the thread does die and not remain static).

The main app is only used to respond to an event i.e the double-click

When the event is fired, I need to create a new fixed size form. The form has to be inside a thread so it doesn't cause the main app to wait while the new form is updating etc (if anyone knows a better approach please let me know).

The threaded form should take the filename that is passed to it and display a list of attributes, updating them every few seconds.

The main purpose is to stop one sub form interferring with another and to stop it causing the main app to hang while it waits for the update timer event to complete.

I was under the assumption that by using threads, I could free up the main app and let it continue loading files or handling double-click events.

Thanks in advance guys,


RE: WinForms multithreading

I think you missed the point of our posts...

Your GUI items should always be on the same thread - work that is needed to be done before updating the GUI should be done in a separate thread so the form/app doesn't wait for the work to be done.

  Main Thread
      |            Second Thread
      |              |
      |              |
      |              |
      |            Work Done
      |             Update Main Thread
      |              |
    Form2 is updated

RE: WinForms multithreading


Makes it clear now. Will this work with a dynamic form?

At the moment I have a function that generates the form programmatically each time the user double-clicks a file name (only one form per file).

I am assuming I simply need to display the form and get my second thread to do the work (refresh the file information)?

I need the popup forms to update based on a timer, have been looking into Threading.Timer's however not too sure how to do it to fit around the programmatically created forms.  Only examples I have managed to get working involve a main form and then doing some event based on a timer i.e. don't involve dealing with a form generated at run time.

Thanks for all the help,


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close