Threading Error
-
I am getting
An object reference is required for the nonstatic field, method, or property 'AMPE_Server.AMPE_Service.ReadRequest()'
in the following method.private static void ProcessRequest() { while ( true ) { Thread requestThread = new Thread( new ThreadStart( ReadRequest ) ); requestThread.Start(); } }
which refers to the following methodprivate void ReadRequest() { AMPE_Server.Communication.Request request = new AMPE_Server.Communication.Request() lock (this) { request = (AMPE_Server.Communication.Request) queueRequests.Dequeue(); } }
What the hell have I done wrong? This learning threading is hard as hell after programming with VB for the past 10 years.
Yes, I program in VB6, but only because I use it to fill my addiction to having a dry place to sleep and food to eat! -
I am getting
An object reference is required for the nonstatic field, method, or property 'AMPE_Server.AMPE_Service.ReadRequest()'
in the following method.private static void ProcessRequest() { while ( true ) { Thread requestThread = new Thread( new ThreadStart( ReadRequest ) ); requestThread.Start(); } }
which refers to the following methodprivate void ReadRequest() { AMPE_Server.Communication.Request request = new AMPE_Server.Communication.Request() lock (this) { request = (AMPE_Server.Communication.Request) queueRequests.Dequeue(); } }
What the hell have I done wrong? This learning threading is hard as hell after programming with VB for the past 10 years.
Yes, I program in VB6, but only because I use it to fill my addiction to having a dry place to sleep and food to eat!private void ReadRequest() here is your problem: you need to have a static function to use threads, as they are independent proccess so write private static void ReadRequest() instead, ofcourse you have to change your code inside the function too since the method is now static. Hope that helps! flow
-
I am getting
An object reference is required for the nonstatic field, method, or property 'AMPE_Server.AMPE_Service.ReadRequest()'
in the following method.private static void ProcessRequest() { while ( true ) { Thread requestThread = new Thread( new ThreadStart( ReadRequest ) ); requestThread.Start(); } }
which refers to the following methodprivate void ReadRequest() { AMPE_Server.Communication.Request request = new AMPE_Server.Communication.Request() lock (this) { request = (AMPE_Server.Communication.Request) queueRequests.Dequeue(); } }
What the hell have I done wrong? This learning threading is hard as hell after programming with VB for the past 10 years.
Yes, I program in VB6, but only because I use it to fill my addiction to having a dry place to sleep and food to eat!This isn't a threading problem and you don't need static methods to use threads. What you've done wrong is that you pass the ReadRequest method to the ThreadStart delegate without using an object.
Thread requestThread = new Thread( new ThreadStart( ReadRequest ) );
As the ReadRequest method isn't static this fails. Either declare your method static or use an object.Thread requestThread = new Thread( new ThreadStart( obj.ReadRequest ) );
www.troschuetz.de -
private void ReadRequest() here is your problem: you need to have a static function to use threads, as they are independent proccess so write private static void ReadRequest() instead, ofcourse you have to change your code inside the function too since the method is now static. Hope that helps! flow
flow5555 wrote: you need to have a static function to use threads, as they are independent proccess That's not the case! www.troschuetz.de
-
private void ReadRequest() here is your problem: you need to have a static function to use threads, as they are independent proccess so write private static void ReadRequest() instead, ofcourse you have to change your code inside the function too since the method is now static. Hope that helps! flow
Thanks for the help. I redefined the method as static and had to remove the lock code. Can you explain what the lock command does? Regards, Eric C. Tomlinson
Yes, I program in VB6, but only because I use it to fill my addiction to having a dry place to sleep and food to eat!