Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WinForms multithreading

Status
Not open for further replies.

tahirk

Programmer
Oct 24, 2005
47
GB
Hi,

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.

Fz
 
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

 
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 first
 
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.
 
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,

Fz
 
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
|
Form1
|
|
Form2.Show()
| Second Thread
| |
|---------------
| |
| |
| Work Done
| Update Main Thread
| |
|<---Invoke-----
|
Form2 is updated
|
 
Thanks,

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,

Fz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top