dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Asynchronously moving file(s) to Recycle Bin

  1. #1
    Join Date
    Aug 2002
    Posts
    707

    Asynchronously moving file(s) to Recycle Bin

    Hi,
    It looks like SHFileOperation() is doing it synchronously.
    Is it possible to do that asynchronously, as the subject says?

    Thank you.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,063

    Re: Asynchronously moving file(s) to Recycle Bin

    Can you run shfileoperation on a background thread?

  3. #3
    Join Date
    Aug 2002
    Posts
    707

    Re: Asynchronously moving file(s) to Recycle Bin

    Hi,
    Quote Originally Posted by Arjay View Post
    Can you run shfileoperation on a background thread?
    I'm trying to write a re-usable component, which maybe used by different applications.
    And I'm sure there is an API which will allow me to just call SHFileOperation asynchronously...

    Besides I'm not very familiar with the MT-programming...

    Thank you.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,063

    Re: Asynchronously moving file(s) to Recycle Bin

    Quote Originally Posted by OneEyeMan View Post
    And I'm sure there is an API which will allow me to just call SHFileOperation asynchronously....
    The SHFileOperation function has been around for a long time, so if there isn't an async version by now, it is unlikely there ever will be. As far as a ready made function that you can use to call SHFileOperation asynchronously... there might be but calling it async probably isn't going to work for you without being able to interact with it from your main thread. I mean, sure you can fire it off, but you'll need to get notified when it completes and whether it completed without errors.

  5. #5
    Join Date
    Aug 2002
    Posts
    707

    Re: Asynchronously moving file(s) to Recycle Bin

    Hi,
    Quote Originally Posted by Arjay View Post
    The SHFileOperation function has been around for a long time, so if there isn't an async version by now, it is unlikely there ever will be. As far as a ready made function that you can use to call SHFileOperation asynchronously... there might be but calling it async probably isn't going to work for you without being able to interact with it from your main thread. I mean, sure you can fire it off, but you'll need to get notified when it completes and whether it completed without errors.
    I think this paragraph from MSDN makes it possible to check the result. Am I missing something?

    It is good practice to examine the value of the fAnyOperationsAborted member of the SHFILEOPSTRUCT. SHFileOperation can return 0 for success if the user cancels the operation. If you do not check fAnyOperationsAborted as well as the return value, you cannot know that the function accomplished the full task you asked of it and you might proceed under incorrect assumptions.
    Thank you.

  6. #6
    Join Date
    Sep 2019
    Posts
    2

    Re: Asynchronously moving file(s) to Recycle Bin

    I'd suggest you consider looking into std::async, and the related std::future features of C++, and start to become comfortable with threading.

  7. #7
    Join Date
    Aug 2002
    Posts
    707

    Re: Asynchronously moving file(s) to Recycle Bin

    Hi,
    Quote Originally Posted by Niccolo View Post
    I'd suggest you consider looking into std::async, and the related std::future features of C++, and start to become comfortable with threading.
    Which standard std::async is available under? I need it to work under C++98.
    Also, asynchronous call is not the same as using threads.

    Thank you.
    Last edited by OneEyeMan; September 4th, 2019 at 11:29 AM.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,816

    Re: Asynchronously moving file(s) to Recycle Bin

    std::async is c++11 - so won't work with c++98. As the current standard is c++17, have you considered upgrading your compiler - as it's about 20 years out of date!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.2.5)

  9. #9
    Join Date
    Aug 2002
    Posts
    707

    Re: Asynchronously moving file(s) to Recycle Bin

    2kaud,
    I'm not there yet, unfortunately.

    Thank you.

  10. #10
    Join Date
    Sep 2019
    Posts
    2

    Re: Asynchronously moving file(s) to Recycle Bin

    "asynchronous call is not the same as using threads."

    Notice the name: std::async

    ...and yes, asynchronous work is done in a thread, whether it is the application starting a thread or a feature of the operating system's method of calling (like overlapped I/O).

    The difference here is that if the Windows API doesn't offer it's own asynchronous call to the feature, that becomes incumbent upon the application to choose to make it asynchronous.

    I really have to question any wisdom behind sticking to C++98 at this point, but for whatever reason you can't use C++11 (which itself is already 8 years old), you'd have to consider the older style of launching a thread using OS services.

    It so happens that std::async came to C++ by way of the Boost libraries, but I can't say if there is any potential of using boost with a C++98 compiler, even in an older version.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,063

    Re: Asynchronously moving file(s) to Recycle Bin

    Quote Originally Posted by OneEyeMan View Post
    Hi,


    I think this paragraph from MSDN makes it possible to check the result. Am I missing something?



    Thank you.
    IIRC, when you programmatically call shfileoperation there is an option to present a progress dialog to the use which the user can cancel. The bool check is for after the synchronous function has returned (so you knoe the user canceled). This doesn't imply shfileoperation is asynchronous though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)