LPT Listener using thread
-
Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.
-
Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.
I never heard about a function called ReadLPT (and google wasn't of any help), from where is that coming from ? Can you give more details ? Anyway, for serial communication (so COM ports), you can use functions like WaitCommEvent. I suppose there should be something similar for you.
Cédric Moonen Software developer
Charting control [v1.2] -
I never heard about a function called ReadLPT (and google wasn't of any help), from where is that coming from ? Can you give more details ? Anyway, for serial communication (so COM ports), you can use functions like WaitCommEvent. I suppose there should be something similar for you.
Cédric Moonen Software developer
Charting control [v1.2] -
Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.
Hi, I don't know much about LPT signal. but i got simmiler problem in one of my project. you do one thing begin main thread once some where in Oninitdialog() or On InitInstance().In main thread function in while(1) loop call child thread & do your work in child thread & you must end that childthread in that childthread itself. have a look on given sample.... OnInitInstance() { begin MyMainthread()... } void MyMainthread() { while(1) { begin mychildthread() ::Sleep(400); } } void mychildthread() { //Do your work here.... ........ ..... _EndThread(); } I hope this will reduce your CPU Uses..... :| Rahul Vaishnav
-
Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.
TPN wrote:
This loop cause the CPU usage always 100%...
And rightly so since you are not yielding control of the CPU. Consider a message pump/loop.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Dear Moonen, Thank for your answer. But I need to clarify that ReadLPT is just only my pseudo code - the code that is responsible for reading LPT port signal/information. Thanks.
Ok but that doesn't help. Which functions are you using exactly to access the port ?
Cédric Moonen Software developer
Charting control [v1.2] -
Ok but that doesn't help. Which functions are you using exactly to access the port ?
Cédric Moonen Software developer
Charting control [v1.2] -
Hi, I don't know much about LPT signal. but i got simmiler problem in one of my project. you do one thing begin main thread once some where in Oninitdialog() or On InitInstance().In main thread function in while(1) loop call child thread & do your work in child thread & you must end that childthread in that childthread itself. have a look on given sample.... OnInitInstance() { begin MyMainthread()... } void MyMainthread() { while(1) { begin mychildthread() ::Sleep(400); } } void mychildthread() { //Do your work here.... ........ ..... _EndThread(); } I hope this will reduce your CPU Uses..... :| Rahul Vaishnav
-
Thanks for your reply. But the problem is the thread cannot sleep every 400ms before checking LPT port. The application must ensure that, where the LPT signal changes, it will immediately handle that.
If the particular pin isn't one of the ones that is monitored by WaitCommEvent, there is no built-in notification mechanism. Short of writing a parallel port driver to implement notification, try putting your busy-wait loop into a seperate thread that runs at a lower priority than the rest of your program. Judy
-
Thanks for your reply. But the problem is the thread cannot sleep every 400ms before checking LPT port. The application must ensure that, where the LPT signal changes, it will immediately handle that.
-
Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.
I have used this code in my project for diduction of CPU uses.. it is working fine at my end. if you dont want to use Sleep() then remove it & add WaitForSingleObject() as shown in given sample. try it....i hope it will work... while(1) { HANDLE hThread; hThread = (HANDLE)_beginthread(CallchildFunction,0,NULL); WaitForSingleObject(hThread, INFINITE); ::Sleep(500); } :| Rahul Vaishnav