Ok so, the program
I created needs to launch a variable amount of threads between 300-600
and be able to close individual threads/all threads at any time.
Boolean checking is not an option - not all threads will close by the
time new threads start and the program will not function. I'm looking
for a way to give each thread an id and store it in an array parallel
to variables_to_monitor so that the indexes match up and can be used to
look up the id based on the index and terminate. Any ideas/help would
be wonderful!!
private bool[] = new bool[0x301];
private Thread ThrScanner;
private int[] variables_to_monitor;
private int argument_to_pass;
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i <= 0x300; i++)
{
if (variables_to_monitor[ i ] != 0 & !bool[ i ])
{
bool[ i ] = true;
arguement_to_pass = i;
ThrScanner = new Thread(Monitor);
ThrScanner.IsBackground = true;
ThrScanner.Start();
}
}
}
private void Monitor();
{
while (true)
{
int compare_variables = variables_to_monitor[arguement_to_pass];
while(compare_variables == variables_to_monitor)
{ System.Threading.Thread.Sleep(10); }
// variables changed, event triggered
}
}
private void button_Click(object sender, System.EventArgs e)
{
ThrScanner.Abort();
bool[] = new bool[0x301];
ThrScanner.Join();
}
Loading
Thomas SieverdingPosted Aug 20, 2007, 2:21 PM
StonePianoPosted Aug 20, 2007, 10:59 AM
ArrayList myThreads = new ArrayList();
...
myThreads.Add(eachNewThread);
You can loop through the threads at anytime with:
foreach (Thread thisThread in myThreads)....
If you need to stop any given thread, then you presumably already have reference to that thread.
When I am managing a large number of complex objects like threads, I design an interface (if they are differing but similar) or an object inheriting from whatever (Thread in this case). Then I can code in any attributes or methods to support the things I want to do with all of them.