views:

119

answers:

6
+2  Q: 

threading in c#

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?

+4  A: 

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.

ChrisF
+2  A: 

Missing using System.Threading in your code file.

Alan
+1  A: 

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.

Itay
+1  A: 

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

alex
+2  A: 

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?

Jon Skeet
I think `ThreadWork` comes from some of the example code on the MSDN, for example on this page - http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx
ChrisF
@ChrisF: Yes - my point is that it's harder to say what the code should actually be, as we don't know what the OP's new thread is meant to do.
Jon Skeet
jon ,actually at what speed you type ?
Srinivas Reddy Thatiparthy
+3  A: 

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.

chibacity
Wow - I'd assumed that bit was just a matter of posting snippets of the code :)
Jon Skeet
I'm with Jon on that one.
ChrisF
"You're not reading the error message." well I did :)
chibacity