Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ending thread and ReadFile is still trying to read

Ending thread and ReadFile is still trying to read

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Janine
    wrote on last edited by
    #1

    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

    D M 2 Replies Last reply
    0
    • J Janine

      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

      D Offline
      D Offline
      Daniel Lohmann
      wrote on last edited by
      #2

      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 )

      J 1 Reply Last reply
      0
      • D Daniel Lohmann

        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 )

        J Offline
        J Offline
        Janine
        wrote on last edited by
        #3

        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

        J D 2 Replies Last reply
        0
        • J Janine

          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

          J Offline
          J Offline
          Janine
          wrote on last edited by
          #4

          Well, I desided to use TerminateThread, even though it is no-no. In this case it can't really make any damage. -Janetta

          1 Reply Last reply
          0
          • J Janine

            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

            D Offline
            D Offline
            Daniel Lohmann
            wrote on last edited by
            #5

            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 )

            J 1 Reply Last reply
            0
            • D Daniel Lohmann

              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 )

              J Offline
              J Offline
              Janine
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • J Janine

                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

                M Offline
                M Offline
                Max Santos
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups