CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    4

    Controlling Thread Count without locking the GUI

    Hi All,

    I am working on a project that runs several processes, each in its own thread. If I run every process at once I can get out of memory errors. What I would like to do is limit the running threads to say 2. I use the following to run each thread
    RunningThreads++;
    Thread mythread = new Thread(new ThreadStart(this.MyFunction1));
    mythread.IsBackground = true;
    mythread.Start();

    For this example heres what each thread does.
    Start Thread 1 which displays text to textbox1
    Start Thread 2 which displays text to textbox2
    Start Thread 3 which displays text to textbox3
    Start Thread 4 which displays text to textbox4

    So if I limit it to 2 running theads it should run 1 and 2, then wait for one of those two threads to complete before proceeding to 3, then wait for a thread to complete again and continue with 4 and so on. However I do not want to lock the gui (not responding).

    What I have tried is before each line putting a while TotalThreads<=MaxThreads loop but that locks the gui where if becomes unresponsive and goes all dim not responding. So I also tried putting that in a function and calling it as a different thread again and setting the background to false, but that did not wait.

    Any ideas would be much appreciated.
    Thanks

  2. #2
    Join Date
    Jan 2013
    Posts
    4

    Re: Controlling Thread Count without locking the GUI

    Got this working using Application.DoEvents(); in a loop

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Controlling Thread Count without locking the GUI

    Just be aware of the relevant issues: http://www.codinghorror.com/blog/200...ents-evil.html
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    52

    Re: Controlling Thread Count without locking the GUI

    This is what a ThreadPool is for: http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

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

Featured