Thread: cancel a DLL call

  1. #1
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76

    cancel a DLL call

    Hi all,

    I know this is a c forum and all but this is worth asking. Anyways I made a DLL in C++ that changes the owner of certain files (might be as many as a few hundred thousand files...) so the DLL call may take a long time to finish and I want to have a cancel feature. The DLL is called from VB6.

    I thought I could pass a boolean to the DLL by reference and change that variable in VB when the user clicks the cancel button. Check that variable in the DLL during the loop and exit the function call if the boolean variable is true.

    This seems kinda hackish to me, and dont really like the idea. Is there a better way of doing it?
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't think that's a bad idea at all. It's probably important to mention that keeping that DLL call in a separate thread is going to be a necessity. Otherwise, your GUI will freeze up and you won't be able to cancel anyway.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Thx a million, I had forgotten/[didn't really know] 'bout the whole multi-threading part!
    Last edited by ryan_germain; 08-03-2006 at 10:24 AM.
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, the long running task should be launched in a new thread. The cancel function can signal it to shut with an event as discussed in this thread. You will also need a system to notify the calling program when the task is complete. This could consist of a polling function (ie. IsTaskFinished) or a callback function.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by anonytmouse
    Yes, the long running task should be launched in a new thread. The cancel function can signal it to shut with an event as discussed in this thread. You will also need a system to notify the calling program when the task is complete. This could consist of a polling function (ie. IsTaskFinished) or a callback function.
    You could do that. You could also use a CRITICALSECTION around the flag.... although there really isn't a major thread safety concern for a flag that is read-only on one side.

    As to the notification of completion, A PostMessage back to the window might work for you.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Well my local guru informed me VB does not support multi-threading. So just to let u guys know, I'm gonna make an ActiveX EXE in VB, this will call the C++ DLL because it will be in a different process. Make a refeference to the ActiveX EXE in the main VB program. I can raise an event in the ActiveX EXE when the DLL function is done and deal with the event in my main program. I know this is all in the VB side so no one really cares but you know...
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I would imagine that the C dll could spawn the thread.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So it would. It's something I've done before which works quite well. Just don't call a callback function from that thread, the behaviour is effectively undefined.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by FillYourBrain
    I would imagine that the C dll could spawn the thread.
    I dont think I quite understand, maybe cause Im new at this. The thing is, if I spawn a new thread in a function in the DLL, will that function return to VB right away?

    I tried a quick google search, didnt find much. Maybe if someone can direct me to some C code samples or tuts about multi-threading.

    thx
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes, the thread will start. The main thread will return to VB. When the thread is done, you can signal (somehow) back to the main window to tell it that you are finished. I might do a PostMessage() call. When the vb window receives the message it can do whatever it would have done after the call originally.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    ok got it, thx
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 02:54 PM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  5. Function Call From DLL
    By (TNT) in forum Windows Programming
    Replies: 5
    Last Post: 05-05-2002, 09:33 PM