Threading in C # creates 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 many errors appear:

Error 2 The type or namespace name 'ThreadStart' was not found (do you miss the use directive or 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 (do you miss the use directive or assembly reference?) C: \ Users \ alexluvsdanielle \ AppData \ Local \ Temporary Projects \ WindowsFormsApplication1 \ Form1.cs 32 31 WindowsFormsApplication1

Error 5 The field initializer cannot refer to a 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?

+4
source share
6 answers

Perhaps I mention that bleedin is explicit here, but from the sample code, your code block is:

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

Not in the method. I have done this before.

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

+6
source

Thread and ThreadStart are in the System.Threading MSDN page .

Add using System.Threading; in the namespace at the top of the code.

ThreadWork not a class defined in .NET. I found the MSDN page here , where it was used in the sample code. Therefore, you need to replace it with the name of your class, where you defined your DoWork method. If it is in the same class as this code, you just need to go through DoWork .

+6
source

Missing using System.Threading in your code file.

+2
source

Others gave a solution to this particular problem, but the real answer to the question "What am I doing wrong?" is (IMO) "You are not reading the error message." There are error messages for some reason: like tips to tell you what is wrong.

You have several error messages:

The type or namespace name 'Fpp' could not be found (can't you see using the directive or assembly link?)

The error message gives you a hint here: it cannot find the type, but maybe because you do not have the correct references or using directives. So check your links and check your using directives. In this case, Thread is in mscorlib , so you are unlikely to have a problem with the assembly reference, so the next check is your using directives. It is in the System.Threading namespace, so you need to

 using System.Threading; 

at the top - or, of course, you can just point System.Threading.Thread everywhere, but it's a little long.

Reading error messages usually means you don’t have to ask what you are doing wrong: they will tell you how they did it.

Correcting a ThreadWork error ThreadWork more difficult to determine - you must use any method that you want to execute in a new thread ... if you really want to start a new thread yourself. Are you really sure?

+2
source

first make sure you have

 using System.Threading; 

at the top of the file.

In addition, you cannot call myThread.Start(); outside the method.

+1
source

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

+1
source

Source: https://habr.com/ru/post/1312694/


All Articles