Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2008, 11:14 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default Getting Threads to work in C#

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:

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!
__________________
What you don\'t know can hurt you!
 
Old April 23rd, 2008, 12:35 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old April 23rd, 2008, 01:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The ThreadStart contructor expects the method name, and not a call to the method:

Code:
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 -/
 
Old April 23rd, 2008, 01:59 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old April 28th, 2008, 10:43 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

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!
 
Old April 28th, 2008, 11:23 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Worker Threads dill BOOK: Professional Microsoft Robotics Studio ISBN: 978-0-470-14107-6 4 June 23rd, 2008 08:42 PM
Struts and Threads varun_java Apache Tomcat 0 April 2nd, 2005 11:01 AM
Threads using ASP nsakic SQL Server ASP 0 May 19th, 2004 12:39 AM
Threads Help hybrid_dev C# 0 July 31st, 2003 10:23 AM
Problems with threads. Threads disappearing? DriesNeyrinck VB.NET 0 June 10th, 2003 08:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.