|
-
Mar 15th, 2004, 04:30 PM
#1
Thread Starter
Lively Member
Thread Error -Pls help
Hello Everybody
I am trying to define a thread process as a bool .But getting erro as Method name expected... Pls guide what is the problem???
private bool abClientProcess;
tcpThd=new Thread(new ThreadStart(ClientProcess( abClientProcess)));
public void ClientProcess( bool abClientProcess)
{
// Code Write to connect to the server.....
}
Pls guide..
Thanks,
Rahil
-
Mar 15th, 2004, 09:14 PM
#2
Hyperactive Member
The constrctor for a Thread class accepts a ThreadStart delegate. Whatever method you pass to Thread constructor must match the ThreadStart delegate. The ThreadStart delegate has no parameters, and your ClientProcess method does; you need to remove the param from that method. If you need to pass values into your ThreadStart delegate you can do something like this:
PHP Code:
using System;
using System.Threading;
namespace Tester
{
public class MainApp
{
public static void Main( string[] args )
{
Worker worker = new Worker("Chris");
Console.WriteLine("Before: Worker valid? {0}", worker.Valid);
Thread t = new Thread(new ThreadStart(worker.DoSomething));
t.Start();
while(t.IsAlive && !worker.Valid)
{
Thread.Sleep(200);
}
Console.WriteLine("After: Worker valid? {0}", worker.Valid);
Console.WriteLine("Done...");
Console.ReadLine();
}
}
public class Worker
{
private string _name = "";
private bool _valid = false;
public bool Valid
{
get
{
return _valid;
}
set
{
_valid = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public Worker(string name)
{
this._name = name;
}
public void DoSomething()
{
this._valid = true;
Console.WriteLine("Hello {0} from the ThreadStart delegate", this.Name);
}
}
}
-
Mar 16th, 2004, 12:15 PM
#3
Thread Starter
Lively Member
THanks
Thanks for the information..
Rahil
Originally posted by pvb
The constrctor for a Thread class accepts a ThreadStart delegate. Whatever method you pass to Thread constructor must match the ThreadStart delegate. The ThreadStart delegate has no parameters, and your ClientProcess method does; you need to remove the param from that method. If you need to pass values into your ThreadStart delegate you can do something like this:
PHP Code:
using System;
using System.Threading;
namespace Tester
{
public class MainApp
{
public static void Main( string[] args )
{
Worker worker = new Worker("Chris");
Console.WriteLine("Before: Worker valid? {0}", worker.Valid);
Thread t = new Thread(new ThreadStart(worker.DoSomething));
t.Start();
while(t.IsAlive && !worker.Valid)
{
Thread.Sleep(200);
}
Console.WriteLine("After: Worker valid? {0}", worker.Valid);
Console.WriteLine("Done...");
Console.ReadLine();
}
}
public class Worker
{
private string _name = "";
private bool _valid = false;
public bool Valid
{
get
{
return _valid;
}
set
{
_valid = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public Worker(string name)
{
this._name = name;
}
public void DoSomething()
{
this._valid = true;
Console.WriteLine("Hello {0} from the ThreadStart delegate", this.Name);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|