Results 1 to 8 of 8

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    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 by Shaitan00; Jul 19th, 2007 at 05:57 PM.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    Use the backgroundworker control
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    Isn't that only for .NET 2.0? Or is it also available in .Net 1.1 (C# 2002)?
    Can you possibly provide more details - never worked with "backgroundworker"'s before...

    Thanks,

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

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

    Quote Originally Posted by Shaitan00
    Isn't that only for .NET 2.0? Or is it also available in .Net 1.1 (C# 2002)?
    Can you possibly provide more details - never worked with "backgroundworker"'s before...

    Thanks,
    Sorry, but the BackGroundWorker only became available in 2.0.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    Is a thread therefore the only way to go?
    Process was great because I could wait for it to exit, get the return code, etc... and the process simply launches an executable file (run.exe) so I didn't think threading was absolutly necessary for this requirement...

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    You'll need another thread(s) to start and control your process, i.e. you'll use both concepts (threads & processes).
    Using multi-threading might cause you some UI problems, so make sure you do a research before you dig into coding
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    See - I get the fact of using a thread if I am to be making changes on the form (frmMain) while the process is running - but in this case I am only doing one thing at a time (which is why I thought using a process would be fine) - the UI is just there displaying to the user but no changes are happening on it...

    So it is normal that when I .Start() my process the form starting it gets automatically hidden?

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

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

    Quote Originally Posted by Shaitan00
    So it is normal that when I .Start() my process the form starting it gets automatically hidden?
    To hide the form is not usual but losing focus is normal
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width