One doubt
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
You can use asynchronous read here. Since you have not mentioned how you are reading data, I will assume you can use the file manipulation functions on some sort of handle or you are using sockets. If you are accessing a handle can use
ReadFile
orReadFileEx
with anOVERLAPPED
structure. For sockets you can useWSARecv
function with anOVERLAPPED
structure. After calling the function you can wait on the event handle inside theOVERLAPPED
structure.«_Superman_» I love work. It gives me something to do between weekends.
-
You can use asynchronous read here. Since you have not mentioned how you are reading data, I will assume you can use the file manipulation functions on some sort of handle or you are using sockets. If you are accessing a handle can use
ReadFile
orReadFileEx
with anOVERLAPPED
structure. For sockets you can useWSARecv
function with anOVERLAPPED
structure. After calling the function you can wait on the event handle inside theOVERLAPPED
structure.«_Superman_» I love work. It gives me something to do between weekends.
I am reading data from the COM port.It is happening in a thread and it keep on reading the data and this data is stored in one CMainFrame variable. My part is to get this variable in my class and do some manipulations after the data has come. The only thing i have to do is to check the data has come or not.
-
I am reading data from the COM port.It is happening in a thread and it keep on reading the data and this data is stored in one CMainFrame variable. My part is to get this variable in my class and do some manipulations after the data has come. The only thing i have to do is to check the data has come or not.
Open the port with the overlapped flag.
CreateFile(_T("COM1"), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
Post the asynchronous read.
OVERLAPPED over = { 0 };
over.hEvent = ::CreateEvent(0, 0, 0, 0);
DWORD dwRead = 0;
ReadFile(m_hPort, buffer, len, &dwRead, &over);Wait on the event for 3 seconds.
if (WAIT_OBJECT_0 != WaitForSingleObject(over.hEvent, 3000))
return;
GetOverlappedResult(m_hPort, &over, &dwReade, 0);«_Superman_» I love work. It gives me something to do between weekends.
-
Open the port with the overlapped flag.
CreateFile(_T("COM1"), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
Post the asynchronous read.
OVERLAPPED over = { 0 };
over.hEvent = ::CreateEvent(0, 0, 0, 0);
DWORD dwRead = 0;
ReadFile(m_hPort, buffer, len, &dwRead, &over);Wait on the event for 3 seconds.
if (WAIT_OBJECT_0 != WaitForSingleObject(over.hEvent, 3000))
return;
GetOverlappedResult(m_hPort, &over, &dwReade, 0);«_Superman_» I love work. It gives me something to do between weekends.
Thanks.
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
Where does the data come from? For most input devices, there is some way of blocking until the input device has some data, rather than having to sleep. But we can't help you if you don't give us that detail!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu
Assuming your data is coming via the serial port, read here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons