Results 1 to 17 of 17

Thread: In Need of Help Checking if object is deleted

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    In Need of Help Checking if object is deleted

    Hey guys,

    I am wracking my brains over trying to figure out what I assume should be simple. In my example code below that is running in a backgroundworker thread, I am getting selected usernames from a list box and attempting to delete them. If the delete is successful, I would like to print Profile {profile} succcessfully deleted, and if the delete is not successful, I would like to print Profile {profile} is locked. How would I go about doing this, as .Delete() throws an exception if the profile can’t be deleted, and I cannot do backgroundworker.reportprogress from inside an exception?

    Code:
    Dim resObjColl As ManagementObjectCollection
    Dim resObj As ManagementObject
     
    For Each profile In ListBoxProfiles.SelectedItems
        resObjColl = WmiConn.ExecWmiQuery($"SELECT * FROM win32_UserProfile WHERE localpath = ""C:\\Users\\{profile}""")
     
        For Each resObj In resObjColl
            resObj.Delete()
            BackgroundWorker4.ReportProgress(10, $"Profile {profile} successfully deleted.{vbCrLf}")
        Next
     
    Next

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Use a Try, catch block inside your inner for each loop

    https://docs.microsoft.com/en-us/dot...ally-statement

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Yeah thats what I was referring to with the reportprogress from within an exception. I can try it and since it doesnt work, I can catch the exception but then I can’t report the progress from within the catch. And if I use catch ex as exception, even with Continue For, WorkerCompleted gets called and if a profile that can be deleted is after the locked profile, it doesnt get deleted.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Code:
    Dim resObjColl As ManagementObjectCollection
    Dim resObj As ManagementObject
     
    For Each profile In ListBoxProfiles.SelectedItems
        resObjColl = WmiConn.ExecWmiQuery($"SELECT * FROM win32_UserProfile WHERE localpath = ""C:\\Users\\{profile}""")
     
        For Each resObj In resObjColl
            Try
                resObj.Delete()
                BackgroundWorker4.ReportProgress(10, $"Profile {profile} successfully deleted.{vbCrLf}")
            Catch ex as exception
    
            Finally
                'report progress for failed delete here
            End Try
        Next
     
    Next

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by chris.martinez View Post
    Yeah thats what I was referring to with the reportprogress from within an exception. I can try it and since it doesnt work, I can catch the exception but then I can’t report the progress from within the catch. And if I use catch ex as exception, even with Continue For, WorkerCompleted gets called and if a profile that can be deleted is after the locked profile, it doesnt get deleted.
    You're saying the first error ends the background thread?

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    See the reason I didnt use a finally statement was that I was under the impression that whatever I put there will execute whether or not the profile gets deleted, so if a profile is deleted wouldn’t the reportprogress happen a second time?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted


  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by chris.martinez View Post
    See the reason I didnt use a finally statement was that I was under the impression that whatever I put there will execute whether or not the profile gets deleted, so if a profile is deleted wouldn’t the reportprogress happen a second time?
    You're probably right about the finally clause

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Thanks for the link, ill check it out when I get home. Its been a pain trying to figure this out lol. I just wanna like attempt to delete the object, and if not successful then continue deleting and be able to somehow check if the object still exists or not. Its super annoying.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Hold on... I got it😁
    You’re deleting part of a collection that you’re looping through in a for each...
    Try a standard for loop, looping from the last item backwards to the first item. Then it will work

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by .paul. View Post
    Hold on... I got it😁
    You’re deleting part of a collection that you’re looping through in a for each...
    Try a standard for loop, looping from the last item backwards to the first item. Then it will work
    So you’re saying loop through the obj collection to delete the object with a standard for loop but starting backwards and it will work? I’m confused why, and to how I could still report the progress if obj.delete() fails.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    Removing items from a collection while looping for, each through that collection doesn't work.
    Why would you want to report - $"Profile {profile} successfully deleted." if the object was not successfully deleted at all?

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by .paul. View Post
    Removing items from a collection while looping for, each through that collection doesn't work.
    Why would you want to report - $"Profile {profile} successfully deleted." if the object was not successfully deleted at all?
    Well I would only want to report that if it was successful, and continue the loop if not while printing profile is locked. I was thinking something like setting an embedded reference or label to 0 at the start of every loop, and if successful then set it to 1 and then check if the reference = 1 then print profile successfully deleted else print profile is locked. Think this would be easier?

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by .paul. View Post
    Removing items from a collection while looping for, each through that collection doesn't work.
    Why would you want to report - $"Profile {profile} successfully deleted." if the object was not successfully deleted at all?
    You mentioning to use the Finally block got me thinking... I want to print a statement whether the delete succeeds or fails, so I figured why not do it in the Finally block as you mentioned, but just check some value I can set depending on whether the object does delete or not.

    So I decided to create a My.Settings boolean reference called isDeleted, and at the beginning of the try statement I set it to false, and right under the Delete() call I set it to true. If the object does delete, then the reference is set to true, and if the object cannot be deleted then an exception is thrown and the code never sets the reference isDeleted to be true. Might not be the most elegant solution, but it works

    Code:
    For Each resObj In resObjColl
        Try
            My.Settings.isDeleted = False
            resObj.Delete()
            My.Settings.isDeleted = True
        Catch ex As Exception
        Finally
            If My.Settings.isDeleted = True Then
                BackgroundWorker4.ReportProgress(20, $"Profile {profile} successfully deleted.{vbCrLf}")
            Else
                BackgroundWorker4.ReportProgress(20, $"Profile {profile} is locked and cannot be deleted.{vbCrLf}")
            End If
        End Try
    
    Next

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    You could just use a Boolean variable, doesn’t need to be My.Settings, which is generally used for persisting data

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    12

    Re: In Need of Help Checking if object is deleted

    Quote Originally Posted by .paul. View Post
    You could just use a Boolean variable, doesn’t need to be My.Settings, which is generally used for persisting data
    True..

    I got stuck on the My.Settings in my head because I needed to store data from another backgroundworker thread and access it from within this backgroundworker thread. Thanks!

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: In Need of Help Checking if object is deleted

    To store and access variables from your gui thread and also your bgw thread you can use form level variables. Google variable scope to see the differences between form level and local variables...

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width