How do I get the readSensor() method to run in threads?

Thread location1Thread;
Thread location2Thread;

Logger location1 = new Logger("", 2000);
Logger location2 = new Logger("", 2000);

// The next 2 lines lines result in the following error message:
// "An object reference is required for the non-static field, method, or property...
location1Thread = new Thread(location1.readSensor);
location2Thread = new Thread(location2.readSensor);

I don't know how to resolve the error. Can anyone help me with this?


Try this:

location1Thread = new Thread(location1.readSensor());
location2Thread = new Thread(location2.readSensor());

Can't use parenthesis there, generates error: "Method name expected" with a red squiggly under location1.readSensor()

Currently the red squiggly is under location1Thread and location2Thread. With the error message noted in my original post.

Thanks anyway...


I solved this problem. The following lines have to be in main(), not in the class that contains main(). I don't understand why.

Thread location1Thread;
Thread location2Thread;



The problem is that you were attempting to access a member variable from a static context (ie the Main method). You need to either use variables local to your static method or move the initialisation of the threads to the constructor and instantiate your class as an object first.