开发者

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\AppDa开发者_运维知识库ta\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?


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.


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.


Missing using System.Threading in your code file.


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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜