2 Replies - 4044 Views - Last Post: 04 July 2012 - 07:29 AM Rate Topic: -----

#1 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

No overload matches delegate 'System.Threading.ThreadStart'

Posted 03 July 2012 - 08:16 PM

Hi again,

I have a static class used for processing server strings:

using System;
using System.IO;
using System.Threading;

namespace AuthServer
{
	public static class HeartbeatHandler
	{
		static int tablesize;
		static bool hasTableSize = false;
		
        private static void getTableSize()
        {
        	tablesize = File.ReadAllLines(Environment.CurrentDirectory + "\\cidata.txt").Length;
        	hasTableSize = true;
        }
        
        public static void Handle(NPHandler client, string data)
        {
        	if (!hasTableSize) {
        		getTableSize();
        	}
        	
        	Log.Data("Received heartbeat: " + data);
            
        	if (!client.RunningHeartbeat) {
            	client.LastHeartbeat = 0;
            	ThreadStart watch = new ThreadStart(WatchClass.WatchHeartbeat);
            	Thread watcher = new Thread(watch);
            	watcher.Start(client);
            	client.RunningHeartbeat = true;
            }
        	
        	string[] splitData = data.Split(' ');
        	
        	client.SessionID = int.Parse(splitData[1]);
        	
        	if (int.Parse(splitData[2]) != 11) {
        	    CIHandler.Handle(client, "CI_Detection " + splitData[2]);
        	}
        	
        	if (int.Parse(splitData[3]) != tablesize) {
        		Log.Data("Sending CIData to client at " + client.Address.ToString());
        		SendCIData(client);
        		client.HasCIData = true;
        	}
        }
        
        private static void SendCIData(NPHandler client)
        {
        	byte[] raw = File.ReadAllBytes(Environment.CurrentDirectory + "\\cidata.txt");
        	
        }
        
        private static byte[] encrypt(byte[] data, byte[] key)
        {
        	return data;
        }
	}
	
	public static class WatchClass
	{
		public static void WatchHeartbeat(NPHandler client)
        {
            while (client != null) {
        		if (client.LastHeartbeat > 89) {
        			client.CloseConnection(false);
        			client.RunningHeartbeat = false;
        			return;
        		}
        		
        		client.LastHeartbeat++;
        		Thread.Sleep(1000);
            }
        }
	}
}



However the line ThreadStart watch = new ThreadStart(WatchClass.WatchHeartbeat); throws an error:
No overload for 'WatchHeartbeat' matches delegate 'System.Threading.ThreadStart' (CS0123) - C:\code\BC SVN\AuthServer\HeartbeatHandler.cs:33,34



Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: No overload matches delegate 'System.Threading.ThreadStart'

#2 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Re: No overload matches delegate 'System.Threading.ThreadStart'

Posted 03 July 2012 - 09:08 PM

Solved by passing arguments in the class constructor.

WatchClass watchobj = new WatchClass(client);
Thread watchThread = new Thread(watchobj.WatchHeartbeat);


Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,861
  • Joined: 02-June 10

Re: No overload matches delegate 'System.Threading.ThreadStart'

Posted 04 July 2012 - 07:29 AM

18 public static void Handle(NPHandler client, string data)

I urge you to use better names. 'Handle' is a truly poor choice for a couple reasons:
1 - It doesn't describe what it handles. btnSend_Click is a handler that describes what it does, as an example. Code maintainability is enhanced when you don't have to guess at method purposes. If you come back to this code in 6 months you'll have no idea what this does.
2 - 'Handle' is a datatype. It would be like naming your method "string" or "double" http://msdn.microsof...1(v=vs.85).aspx
3 - What happens when you want to have more handlers? You can't stick to a consistent naming style unless you do Handler1, Handler2, Handler3 etc. and that is truly fugly.

while 
{
  //bla blah
  //Thread.Sleep(1000);
}


Really bad. Thread.sleep is one of the post rookie-abused codes ever. Amongst other problems in your use of this, it means it will be hard to every give more features to your class because the entire class will spend more time asleep than anything else.
Instead I suggest a timer set for 1000ms. Then do whatever you want in the .Tick handler. That way the thread stays awake.


Why is everything static? Wouldn't you like to some day have additional instances of these so you can have more than one client connecting and so on?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1