Executing 2 Threads

G

Guest

Hello Everyone,

I’m having a bit of difficulty with a threading scenario.
Basically I’m creating 2 threads and trying to execute them one after the
other.
The problem is that the second thread never fires.

My environment is a bit complex, and I’m wondering of that’s what is working
against me. At the top level I have s series of windows services running
which all operate independent of each other. Each of these services operates
on a timer and with each tick event will try to create an instance of a new
“Engine†class. The “Engine†classes that are being created further
instantiate “Helper†classes.

Each “Helper†supports a “Start_Module†method which takes no parameters.
When the “Start_Module†method is called the “Helper†class will perform
various tasks such as updating records in SQL Server and or formatting data.

Each of my “Engine†classes also support a method called “Start_Engineâ€
which is responsible for instantiating my “Helper†classes.

To further complicate things, each “Engine†class also supports “Can_Stopâ€
Boolean method which will look at of each of it’s child threads to see if the
Engine can be unloaded from memory by the controlling Windows service.

So my main problem is that once I create and start the “Engine†class it
will successfully create the 1st thread, but never get around to starting the
2nd thread.

I appreciate any help or advice that you can give me on this matter.
Thanks,

Johnathan Rossitter

Here is an example of the code I’m trying to make work:

using System;
using System.Threading;
namespace Utility
{
public class Engine1: Engine
{
Thread[] MyThreads = new Thread[2];

private Helper1 H1;
private Helper2 H2

public Engine1()
{
}

private void Checker()
{
H1 = new H1();
H1.Start_Helper();
}

private void Cleaner()
{
H2 = new H2();
H2.Start_Helper();
}

public override bool Start_Engine()
{
MyThreads[0] = new Thread(new ThreadStart(Checker));
MyThreads[1] = new Thread(new ThreadStart(Cleaner));

MyThreads[0].Start();
MyThreads[1].Start();

return true;
}

public override bool Can_Stop()
{
bool RetVal = true;
for(int i = 0; i < MyThreads.Length; i++)
{
if(MyThreads.IsAlive == true)
{
RetVal = false;
}
}

return RetVal;
}
}

public class Helper1:Helper
{
public override bool Start_Module()
{
//Do SQL Stuff
//Do Formatting Stuff
return false;
}

public Helper1()
{
}
}

public class Helper2:Helper
{
public override bool Start_Module()
{
//Do SQL Stuff
//Do Formatting Stuff
return false;
}

public Helper2()
{
}
}

}
 
G

Guest

Nevermind,

I found the problem it dealt with an unhandled SQL Exception deep in my
"Helper" class

Thanks anyway

Johnathan

John Rossitter said:
Hello Everyone,

I’m having a bit of difficulty with a threading scenario.
Basically I’m creating 2 threads and trying to execute them one after the
other.
The problem is that the second thread never fires.

My environment is a bit complex, and I’m wondering of that’s what is working
against me. At the top level I have s series of windows services running
which all operate independent of each other. Each of these services operates
on a timer and with each tick event will try to create an instance of a new
“Engine†class. The “Engine†classes that are being created further
instantiate “Helper†classes.

Each “Helper†supports a “Start_Module†method which takes no parameters.
When the “Start_Module†method is called the “Helper†class will perform
various tasks such as updating records in SQL Server and or formatting data.

Each of my “Engine†classes also support a method called “Start_Engineâ€
which is responsible for instantiating my “Helper†classes.

To further complicate things, each “Engine†class also supports “Can_Stopâ€
Boolean method which will look at of each of it’s child threads to see if the
Engine can be unloaded from memory by the controlling Windows service.

So my main problem is that once I create and start the “Engine†class it
will successfully create the 1st thread, but never get around to starting the
2nd thread.

I appreciate any help or advice that you can give me on this matter.
Thanks,

Johnathan Rossitter

Here is an example of the code I’m trying to make work:

using System;
using System.Threading;
namespace Utility
{
public class Engine1: Engine
{
Thread[] MyThreads = new Thread[2];

private Helper1 H1;
private Helper2 H2

public Engine1()
{
}

private void Checker()
{
H1 = new H1();
H1.Start_Helper();
}

private void Cleaner()
{
H2 = new H2();
H2.Start_Helper();
}

public override bool Start_Engine()
{
MyThreads[0] = new Thread(new ThreadStart(Checker));
MyThreads[1] = new Thread(new ThreadStart(Cleaner));

MyThreads[0].Start();
MyThreads[1].Start();

return true;
}

public override bool Can_Stop()
{
bool RetVal = true;
for(int i = 0; i < MyThreads.Length; i++)
{
if(MyThreads.IsAlive == true)
{
RetVal = false;
}
}

return RetVal;
}
}

public class Helper1:Helper
{
public override bool Start_Module()
{
//Do SQL Stuff
//Do Formatting Stuff
return false;
}

public Helper1()
{
}
}

public class Helper2:Helper
{
public override bool Start_Module()
{
//Do SQL Stuff
//Do Formatting Stuff
return false;
}

public Helper2()
{
}
}

}
 

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