BruceTab

Running VS.net 2003 V7.1

Worker workerObject = new Worker();

Thread workerThread = new Thread(workerObject.DoWork);

// Start the worker thread.

workerThread.Start();

Getting the following compile error: CS0654: Method 'TestBatch.Worker.DoWork()' referenced without parentheses

Not sure how to resolve it. Example code shows, I don't need parentheses when instantiating Thread object



Re: Visual C# Language Not clear how to resolve CS0654 'TestBatch.Worker.DoWork()' referenced without parentheses

Peter Ritchie

The Thread constructor expects a ThreadStart (or a ParameterizedThreadStart) delegate, the compiler thinks you're trying to execute the DoWork method and gives you an error that you forgot the parenthesis for the method call. You need to create a new delegate. I don't know what your DoWork method looks like, so I'll assume it's declared as "public void DoWork()" and use TheadStart:

Thread workerThread = new Thread(new ThreadStart(workerObject.DoWork));





Re: Visual C# Language Not clear how to resolve CS0654 'TestBatch.Worker.DoWork()' referenced without parentheses

Peter Ritchie

Sorry, I missed the part about VS 2003. Ignore the comments about ParameterizedThreadStart; you must use ThreadStart, as in my sample code.




Re: Visual C# Language Not clear how to resolve CS0654 'TestBatch.Worker.DoWork()' referenced without parentheses

Figo Fei - MSFT

Hi, Bruce

Tutorials original link: http://msdn2.microsoft.com/en-us/library/7a2f3ay4(VS.80).aspx

The cause is the difference between VS2003 compiler and VS2005 compiler.

see: http://msdn2.microsoft.com/en-us/library/system.threading.threadstart(VS.80).aspx

Note:

Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor.

That is to say, the sample code in the msdn2 works in VS2005(because the compiler allow to omit the simplified syntax according to the code context), however, in 2003 you can not omit the ThreadStart or ParameterizedThreadStart delegate in constructor.

So you should use

Thread workerThread = new Thread(new ThreadStart(workerObject.DoWork));

instead of

Thread workerThread = new Thread(workerObject.DoWork);

Thank you