Alex Gordon
Alex Gordon

Reputation: 60691

Threading in C# produces errors

I am using this code:

private void Form1_Load(object sender, EventArgs e)
{

}

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string response = serialPort1.ReadLine();
    this.BeginInvoke(new MethodInvoker(
        () => textBox1.AppendText(response + "\r\n")
    ));
}

ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

but am getting lots of errors:

Error 2 The type or namespace name 'ThreadStart' could not be found (are you missing a using directive or an assembly reference?) C:\Users\alexluvsdanielle\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 31 44 WindowsFormsApplication1

Error 3 The name 'ThreadWork' does not exist in the current context C:\Users\alexluvsdanielle\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 31 56 WindowsFormsApplication1

Error 4 The type or namespace name 'Thread' could not be found (are you missing a using directive or an assembly reference?) C:\Users\alexluvsdanielle\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 32 31 WindowsFormsApplication1

Error 5 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.myThreadDelegate' C:\Users\alexluvsdanielle\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 32 38 WindowsFormsApplication1

What am I doing wrong?

Upvotes: 4

Views: 5900

Answers (6)

Tim Lloyd
Tim Lloyd

Reputation: 38434

Perhaps I'm mentioning the bleedin' obvious here, but from the code example, your code block:

ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();

Is not in a method. I have done this before.

Once you have fixed this, the other answers will obviously then apply.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1499770

Others have given the solution to this particular problem, but the real answer to "What am I doing wrong?" is (IMO) "You're not reading the error message." Error messages are there for a reason: as hints to tell you what's wrong.

You've got several error messages like this:

The type or namespace name 'Fpp' could not be found (are you missing a using directive or an assembly reference?)

The error message is giving you a hint here: it can't find the type, but that may be because you haven't got the right references or using directives. So check your references, and check your using directives. In this case Thread is in mscorlib so you're very unlikely to have an assembly reference problem - so the next thing to check is your using directives. It's in the System.Threading namespace, so you need

using System.Threading;

at the top - or of course you could just specify System.Threading.Thread everywhere, but that's a bit long-winded.

Reading error messages carefully will usually means you don't need to ask what you're doing wrong: they will tell you, as they have done here.

The fix for the ThreadWork error is harder to determine - you should be using whatever method you want to be executed in the new thread... assuming you actually want to start a new thread yourself. Are you sure you really do?

Upvotes: 2

ChrisF
ChrisF

Reputation: 137108

Thread and ThreadStart are in System.Threading MSDN page.

Add using System.Threading; to the namespaces at the top of your code.

ThreadWork is not a class defined in .NET. I've found an MSDN page here where it's used in the example code. So you need to replace it with the name of your class where you've defined your DoWork method. If it's in the same class as this code then you just need to pass DoWork.

Upvotes: 6

alex
alex

Reputation: 1278

You should put the following code at the top of your C# code-file:
using System.Threading

Upvotes: 1

Itay Karo
Itay Karo

Reputation: 18286

for start make sure that you have a

using System.Threading;

on the top of the file.

In addition - you cannot call myThread.Start(); outside a method.

Upvotes: 1

Alan
Alan

Reputation: 46813

Missing using System.Threading in your code file.

Upvotes: 2

Related Questions