473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Threads deadlocking?

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?
--
Adam Clauss
ca*****@tamu.edu

Nov 16 '05 #1
2 1371
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());
}
}
}
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
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?
--
Adam Clauss
ca*****@tamu.edu

Nov 16 '05 #2
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

--
Adam Clauss
ca*****@tamu.edu
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
1
by: Johnny | last post by:
Hiya, I've been getting some deadlocking that I've never seen before. I rewrote just over 10,000 lines of code, so I can't really go back and comment out what I've done. In a nutshell, could my...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
18
by: Zytan | last post by:
I have multiple threads writing to WebBrowser (using a function that checks InvokedRequired, and if so, invokes itself on the WebBrowser thread) and they are getting deadlocked. They only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.