Threads deadlocking?

A

Adam Clauss

OK, my main thread will create some number of other threads (number determined at runtime). Each of these threads creates a form
via a call:

private void ProcessThread()
{
Application.Run(new MyForm());
}


The problem is, I only want up to 10 of these threads to be actively running at the same time. Initially, I had this implemented
via a ThreadPool, but I needed to control the state of the thread apartment, so I am spawning them all individually. So, I am
trying to come up with another way to control their number.

One result I came up with, was in the form's constructor:
bool run = false;
while (!run)
{
lock (this)
{
run = count < MAX_COUNT;
if (run)
count++;
}
if (!run)
System.Threading.Thread.Sleep(100);
}

where count is a private static int of the form class and MAX_COUNT is a private const int. I added a handler for OnClosing, and
have:
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
lock (this)
{
count--;
}
}

However, it seems a lot of the time some of the threads just kind of get stuckand never run, I'm assuming as a result of something
I'm doing here. Any ideas as to what might be wrong?

Or any better ideas on how to handle it?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Why don't you start the first 10 threads in a loop and then each thread proc
will be resposible to restart the form when it's closed. This way you create
ans reuse only 10 threads.
Look at the sample below.
BTW as far as I remember there was a bug in the framework where one can't
call Application.Run twise in the same thread. It looks like with SP1 it got
fixed. So if you encounter that problem instead of looping in the
UIThreadProc just create and run new thread and let the old one die
peacefully .

using System;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;

namespace ConsoleApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

for(int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(UIThreadProc));
t.ApartmentState = ApartmentState.STA;
t.Start();

}
}

static void UIThreadProc()
{
while(true)
{
Application.Run(new Form());
}
}
}
}
 
A

Adam Clauss

Stoitcho Goutsev (100) said:
Why don't you start the first 10 threads in a loop and then each thread proc
will be resposible to restart the form when it's closed. This way you create
ans reuse only 10 threads.
Ahh, yeah that is a better idea, I'll give that a try.
Look at the sample below.
BTW as far as I remember there was a bug in the framework where one can't
call Application.Run twise in the same thread. It looks like with SP1 it got
fixed. So if you encounter that problem instead of looping in the
UIThreadProc just create and run new thread and let the old one die
peacefully .
Hmm, I'll keep my eye out for that problem.

Thanks for your suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top