Ending thread and ReadFile is still trying to read
-
Hi, I have a worker thread doing a lengthy operation during which it reads data using ReadFile. If I try to abort the operation and call AfxEndThread ReadFile tries to read data until the time limit is reached. Is it possible to stop ReadFile or is there a better way to wait for it to finish than Sleep? The problem with Sleep is that I can't set those time limits myself and I can't be sure how long they are. -Janetta
-
Hi, I have a worker thread doing a lengthy operation during which it reads data using ReadFile. If I try to abort the operation and call AfxEndThread ReadFile tries to read data until the time limit is reached. Is it possible to stop ReadFile or is there a better way to wait for it to finish than Sleep? The problem with Sleep is that I can't set those time limits myself and I can't be sure how long they are. -Janetta
Janetta, I am a bit confused about the scenario you described. Your worker thread calls first ReadFile(), then AfxEndThread() but ReadFile() continues to execute :confused: :confused: Are we talking about one or two threads here? If we are talking about one thread, you are using overlapped IO. In this case you could cancle the IO with CancleIO(). If we are talking about two threads, you misunderstood the function of AfxEndhread(). It could be called only from inside the thread that wants to end and it is not possible to end a foreign thread this way. (Ending a foreign thread would be possible with TerminatThread(), however, using TerminateThread() is a big no-no!) Or am I completly on the wrong path? -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
-
Janetta, I am a bit confused about the scenario you described. Your worker thread calls first ReadFile(), then AfxEndThread() but ReadFile() continues to execute :confused: :confused: Are we talking about one or two threads here? If we are talking about one thread, you are using overlapped IO. In this case you could cancle the IO with CancleIO(). If we are talking about two threads, you misunderstood the function of AfxEndhread(). It could be called only from inside the thread that wants to end and it is not possible to end a foreign thread this way. (Ending a foreign thread would be possible with TerminatThread(), however, using TerminateThread() is a big no-no!) Or am I completly on the wrong path? -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
Actually the first option was what I thought I was doing...:-O I'm trying to make it possible to abort that lengthy operation. A message is posted to the worker thread and I thought that it would call a method of the worker thread that calls AfxEndThread. Posting the message succeedes, but the EndThread method is never called. Is this because the thread is busy performing that lengthy operation and when it is done, the thread is ended? What would be a better way to do this? -Janetta
-
Actually the first option was what I thought I was doing...:-O I'm trying to make it possible to abort that lengthy operation. A message is posted to the worker thread and I thought that it would call a method of the worker thread that calls AfxEndThread. Posting the message succeedes, but the EndThread method is never called. Is this because the thread is busy performing that lengthy operation and when it is done, the thread is ended? What would be a better way to do this? -Janetta
-
Actually the first option was what I thought I was doing...:-O I'm trying to make it possible to abort that lengthy operation. A message is posted to the worker thread and I thought that it would call a method of the worker thread that calls AfxEndThread. Posting the message succeedes, but the EndThread method is never called. Is this because the thread is busy performing that lengthy operation and when it is done, the thread is ended? What would be a better way to do this? -Janetta
A better solution would be to use non-blocking file io (overlapped IO) and wait for the ReadFile() operation in a loop that calls MsgWaitForSingleObject() so outstanding messages are also processed. If you get the message for "end the thing" you cancle the IO via CancelIo() and then just terminate the thread. However, if you are sure that TerminateThread() is acceptable here, it may be the quick, easy and really dirty solution for your problem :~ -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
-
A better solution would be to use non-blocking file io (overlapped IO) and wait for the ReadFile() operation in a loop that calls MsgWaitForSingleObject() so outstanding messages are also processed. If you get the message for "end the thing" you cancle the IO via CancelIo() and then just terminate the thread. However, if you are sure that TerminateThread() is acceptable here, it may be the quick, easy and really dirty solution for your problem :~ -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
Daniel Lohmann wrote: A better solution would be to use non-blocking file io (overlapped IO) and wait for the ReadFile() operation in a loop that calls MsgWaitForSingleObject() so outstanding messages are also processed. Unfortunately I can't do that, because I can't touch the code that is doing the reading. I'll use TerminateThread and keep my fingers crossed:) -Janetta
-
Hi, I have a worker thread doing a lengthy operation during which it reads data using ReadFile. If I try to abort the operation and call AfxEndThread ReadFile tries to read data until the time limit is reached. Is it possible to stop ReadFile or is there a better way to wait for it to finish than Sleep? The problem with Sleep is that I can't set those time limits myself and I can't be sure how long they are. -Janetta
Dont use TerminateThread or something like that... You must use a semaphore The semaphore is used to tell the thread that it must exit the thread must test the state of the semaphore in the ReadFile while and exit if the semaphore tells it to Casa.Sapo.pt