Thanks Rob and Sam for responding!
I was able to get the basic multi-threading working and have since run into additional problems. The solution the the problem above was, as Sam indicated:
Thread t = new Thread(new ThreadStart(MakeThread));
t.Start();
Where the signature for MakeThread is:
private void MakeThread() {...}
MakeThread creates an instance of ProcessTag and calls a method on that instance passing in the necessary data. MakeThread also assists in thread accounting by incrementing the thread counter (t_count) when it starts and decrementing it when it ends.
The method that creates the threads runs a loop keeping track of the created threads as such:
Code:
...
private void GetData() {
...
while(true) {
if(t_count < MAX_THREADS) {
...
if(DoneProcessing) break;
...
Thread t = new Thread(new ThreadStart(MakeThread));
t.Start();
}
Thread.Sleep(15000); // assures each thread startup completes before the next //thread.
}
...
while(t_count > 0) {
Thread.Sleep(1000);
}
}
...
The problem I am having now appears to be related to the number of available threads. Here is the complete error I get:
4/28/2008 9:54:11 AM::System.Threading.SemaphoreFullException: Adding the given count to the semaphore would cause it to exceed its maximum count.
at System.Threading.Semaphore.Release(Int32 releaseCount)
at System.Threading.Semaphore.Release()
at MySql.Data.MySqlClient.MySqlPool.ReleaseConnection (Driver driver)
at MySql.Data.MySqlClient.MySqlPoolManager.ReleaseCon nection(Driver driver)
at MySql.Data.MySqlClient.MySqlConnection.Close()
at PIExtIface.ProcessTag.GetDataForTag(String PITag, String TableName, String PtType, DateTime dtStart, DateTime dtEnd, String Interval, Boolean Average) in C:\Documents and Settings\fairchildd\My Documents\Visual Studio 2005\Projects\PIExtractor\PIExtIface\ProcessTag.cs :line 154
This problem occurs when I "roll over" a thread. For instance, say I have set MAX_THREADS to 5. All 5 threads start up and begin collecting data. When the first thread completes, the instance closes and MakeThread decrements t_count. When the loop in GetData checks t_count, it will be less than MAX_THREADS and a new thread will be started. It is at this time that I get the SemaphoreFullException. Oddly enough, the new thread gets created and collects data, however, no other new threads are created and the application will process the remaining threads.
I have tried implementing Semaphore Thread pooling but I have had problems with that as well. It is not currently implemented so I can't describe the exact problems I have had with it.
Any ideas on why I get a FullSemaphore exception when I haven't implemented Semaphores?
Any other ideas on Threading in .Net?
Thanks for the help.
What you don't know can hurt you!