|
Subject:
|
Getting Threads to work in C#
|
|
Posted By:
|
David_0223
|
Post Date:
|
4/22/2008 11:14:22 PM
|
I have a complex application that I need to speed up and I think multithreading will do the trick, but I don't seem to be able to get it to work.
I am now getting the error "Method name expected"
here is the essence of the code:
namespace MyNamespace {
public struct TagObjec ...
class ProcessTag {
...
public void GetTagData(TagObject Input) {
do stuff with Input
}
...
}
class Form1 {
...
public void MakeThread(object Input)
{
ProcessTag pt = new ProcessTag();
pt.GetTagData((TagObject)Input);
}
...
private void btnGetData_Click(object sender, EventArgs e) {
...
TagObject Input = new TagObject();
fill Input
...
Thread t = new Thread(new ThreadStart(MakeThread((object)Input)));
t.start(Input);
...
}
}
}
The part in bold is identified as the source of the "Method name expected" error.
Any help would be appreciated.
Thanks, Dave
What you don't know can hurt you!
|
|
Reply By:
|
robzyc
|
Reply Date:
|
4/23/2008 12:35:02 AM
|
The error is exactly what the compiler is saying, you need a method name.
I think if you create a delegate with the same signature, and instantiate it as a pointer to the MakeThread(object) method, you then pass the delegate to the [b]ThreadStart[b] method...
Hope this helps 
Rob http://robzyc.spaces.live.com
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/23/2008 1:55:30 AM
|
The ThreadStart contructor expects the method name, and not a call to the method:
Thread t = new Thread(new ThreadStart(MakeThread));
You can then optionally pass an object to this method by calling Start(Object):
t.Start(Input);
/- Sam Judson : Wrox Technical Editor -/
|
|
Reply By:
|
robzyc
|
Reply Date:
|
4/23/2008 1:59:15 AM
|
OK looks like I was way off base on that one! I know we have some multithreaded code in a project I was working on and I am sure delegates were used in it!
Will have to check it out and see what it was doing! 
Thanks Sam
Rob http://robzyc.spaces.live.com
|
|
Reply By:
|
David_0223
|
Reply Date:
|
4/28/2008 10:43:15 AM
|
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:
...
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.ReleaseConnection(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!
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/28/2008 11:23:17 AM
|
Probably better to not reinvent the wheel by doing your own thread pooling.
Just use the ThreadPool class, using the SetMaxThread() and QueueUserWorkItem() methods.
/- Sam Judson : Wrox Technical Editor -/
|