Need to use a thread instead of a process? [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
My goal is to have a FORM always displayed the to user (even though you he can't do anything with it) that contains a listbox that will display information to his - this form (frmMain) will launch "something" like 5 times in a row (one after the other) and inbetween each launch (when the job is complete) I want to display the information in frmMain for the user to see the progress... Pretty simple...

Pseudo-code
Code:
Display ("Starting Processing");
Display ("Starting Task #1");
StartTask(1);
Display ("Finished Task #1");
Display ("Starting Task #2");
StartTask(2);
... etc ...
... etc ...
(where "Display" updates the listbox and "StartTask" launches the task and waits for it to return before continuing).

Now I thought this would have been rather simple to do - "Display" simply add items to the listbox between tasks (never while one is running) and "StartTask" simply started processes (System.Diagnostics.Process() with .WaitForExit) and provided the correct StartInfo. Now the flow works (all the tasks get performed and the text written to the listbox) but the problem occurs WHILE the tasks are running.

Specifically - the form (frmMain) is not displaying information in the ListBox correctly while the process is running - for starters I had to do this.Show() right after I do Process.Start(); or it would hide in the background when I started the process (note - the process itself is hidden correctly) and even then it doesn't display correctly (looks like it is frozen/loading while the process is running). Only when the tasks are done do you see the listbox correctly with all the information. (this is hard to explain).

So - I thougth maybe this could be due to the fact that I am using a PROCESS and not a THREAD to accomplish my task - maybe the form (frmMain) being its own single-thread can't be displaying the GUI and running the process at the same time? (please correct me if I am wrong)...

Anyways - am I correct in assuming that I would need to use a THREAD instead of a PROCESS to solve this problem? Odd because I am not actual processing anything while the process is running (I know I can't update the forms listbox at the same time that I run the process) - I just want the frmMain to be in focus with the correctly display listbox information while the process runs in the background...

Any hints, solutions, ideas, or advice would be much appreciated.
Thanks,
 
Last edited:
Take a look at the background worker class. In order to have a responsive GUI, you do need to have one thread dedicated to updating the GUI and a second thread doing work behind the scenes -- other wise you get an unresponsive GUI.

Depending on what you are doing you will probably call your processes from within the worker thread.

Unless I misunderstood what you were asking, the background worker should get you started.
 
Sadly the concept of a background worker was only introduced in .Net2.0 and I am using .Net1.1 (C# 2002) so I guess I just need to use a thread instead.
Thanks,
 
Back
Top