Click here to Skip to main content
15,890,185 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to wait for thread & if no response within time abort it Pin
David Crow16-Sep-05 2:18
David Crow16-Sep-05 2:18 
AnswerRe: How to wait for thread & if no response within time abort it Pin
Roger Stoltz14-Sep-05 21:45
Roger Stoltz14-Sep-05 21:45 
GeneralRe: How to wait for thread & if no response within time abort it Pin
karmendra_js14-Sep-05 22:04
karmendra_js14-Sep-05 22:04 
GeneralRe: How to wait for thread & if no response within time abort it Pin
HumanOsc14-Sep-05 22:40
HumanOsc14-Sep-05 22:40 
AnswerRe: How to wait for thread & if no response within time abort it Pin
karmendra_js14-Sep-05 23:03
karmendra_js14-Sep-05 23:03 
GeneralRe: How to wait for thread & if no response within time abort it Pin
Roger Stoltz14-Sep-05 23:22
Roger Stoltz14-Sep-05 23:22 
GeneralRe: How to wait for thread & if no response within time abort it Pin
karmendra_js14-Sep-05 23:43
karmendra_js14-Sep-05 23:43 
GeneralRe: How to wait for thread & if no response within time abort it Pin
Roger Stoltz15-Sep-05 1:53
Roger Stoltz15-Sep-05 1:53 
Please note that the following are recommendations and suggestions due to how I interpret the problem and your solution.

Why do I consider this a design problem?
There are two ways of solving problems like this:
1. sequential (blocking calls, synchronous, one thread)
2. parallel (non-blocking calls, asynchronous, multiple threads)

Whenever you are thinking "I start a thread here and wait for it to end" you are trying to solve a parallel problem in a sequential way or vice versa.
This is bad design.
Either you solve it in a sequential way by having a blocking call to your write function and when you have actually written the data it polls for an acknowledge before returning...or
you solve it in a parallel way by having a thread polling for acknowledgements and set an event when such an ack has been received.
In the latter case you will wait for the event to be signaled in your write function with ::WaitForSingleObject or similar. If the device doesn't respond within proper time your wait function will return WAIT_TIMEOUT and you know that the data was not written. You should select your timeout so that the user doesn't feel that the application has hung, e.g. < 1s.

The whole idea with a worker thread is to perform a task in the background and signal the main thread when the job is done, either by setting an event that another thread may be waiting for or posting a message.

If I have understood your problem correctly it all boils down to the fact that you want to write some data and to know if the data was properly written you are waiting for an acknowledge.
I understand this as the core of your problem.
As I understand it the ACK you are waiting for consists of data, i.e. to be able to know if you have received the ACK you have receive data.

What I would do is have a thread that receives data and interprets it, at least to know if the data received is an ACK and if it is use ::SetEvent() to signal an event. In the write function I would write the data and then call ::WaitForSingleObject with the handle of the event mentioned above as input parameter. If ::WaitForSingleObject returns WAIT_TIMEOUT I can return FALSE, -1 or whatever to notify the caller that the operation failed.
The receiving thread will be terminated when the connection to the device is closed.

HumanOsc recommended you an article written by Joseph Newcomer about worker threads. Joe has written lots of stuff that is absolutely worth reading at his own site: http://www.flounder.com/mvp_tips.htm[^]
The essential essays are in my opinion are about worker threads, message management, time management and "The n Habits of Highly Defective Windows Programs" since they address issues that one have to know as a windows developer combined with healthy coding standards and tips.

Hope this helped you
--
Roger
QuestionAbout CString : Plz Send Urgently Pin
parims14-Sep-05 19:08
parims14-Sep-05 19:08 
AnswerRe: About CString : Plz Send Urgently Pin
karmendra_js14-Sep-05 19:45
karmendra_js14-Sep-05 19:45 
AnswerRe: About CString : Plz Send Urgently Pin
prasad_som14-Sep-05 19:51
prasad_som14-Sep-05 19:51 
AnswerRe: About CString : Plz Send Urgently Pin
kakan14-Sep-05 20:03
professionalkakan14-Sep-05 20:03 
AnswerRe: About CString : Plz Send Urgently Pin
vikas amin15-Sep-05 1:11
vikas amin15-Sep-05 1:11 
JokeRe: About CString : Plz Send Urgently Pin
prasad_som15-Sep-05 2:16
prasad_som15-Sep-05 2:16 
AnswerRe: About CString : Plz Send Urgently Pin
YoSilver15-Sep-05 14:35
YoSilver15-Sep-05 14:35 
GeneralRe: About CString : Plz Send Urgently Pin
ThatsAlok15-Sep-05 18:11
ThatsAlok15-Sep-05 18:11 
Questionwhy does only a part of VARIANT change from VT_DATE to VT_BSTR? Pin
liuyue14-Sep-05 18:43
liuyue14-Sep-05 18:43 
Questionwrite Text on sphere Pin
cofd14-Sep-05 18:42
cofd14-Sep-05 18:42 
AnswerRe: write Text on sphere Pin
User 58385214-Sep-05 18:46
User 58385214-Sep-05 18:46 
GeneralRe: write Text on sphere Pin
cofd14-Sep-05 19:00
cofd14-Sep-05 19:00 
GeneralRe: write Text on sphere Pin
cofd14-Sep-05 19:11
cofd14-Sep-05 19:11 
Questiona vss question Pin
followait14-Sep-05 17:39
followait14-Sep-05 17:39 
QuestionI've written a service and it can't access mapped network drives. Why? Pin
zhaopi14-Sep-05 17:26
zhaopi14-Sep-05 17:26 
QuestionSir,can you help me Pin
momer14-Sep-05 14:49
momer14-Sep-05 14:49 
AnswerRe: Sir,can you help me Pin
Christian Graus14-Sep-05 16:30
protectorChristian Graus14-Sep-05 16:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.