-
Nov 10th, 2020, 07:32 PM
#1
Thread Starter
New Member
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
-
Nov 10th, 2020, 07:36 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 07:44 PM
#3
Thread Starter
New Member
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.
-
Nov 10th, 2020, 07:53 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 07:55 PM
#5
Re: In Need of Help Checking if object is deleted
Originally Posted by chris.martinez
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 07:57 PM
#6
Thread Starter
New Member
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?
-
Nov 10th, 2020, 07:59 PM
#7
Re: In Need of Help Checking if object is deleted
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 08:02 PM
#8
Re: In Need of Help Checking if object is deleted
Originally Posted by chris.martinez
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 08:08 PM
#9
Thread Starter
New Member
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.
-
Nov 10th, 2020, 08:23 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 09:01 PM
#11
Thread Starter
New Member
Re: In Need of Help Checking if object is deleted
Originally Posted by .paul.
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.
-
Nov 10th, 2020, 09:26 PM
#12
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 10th, 2020, 09:40 PM
#13
Thread Starter
New Member
Re: In Need of Help Checking if object is deleted
Originally Posted by .paul.
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?
-
Nov 11th, 2020, 04:43 PM
#14
Thread Starter
New Member
Re: In Need of Help Checking if object is deleted
Originally Posted by .paul.
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
-
Nov 11th, 2020, 04:49 PM
#15
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 11th, 2020, 04:52 PM
#16
Thread Starter
New Member
Re: In Need of Help Checking if object is deleted
Originally Posted by .paul.
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!
-
Nov 11th, 2020, 05:36 PM
#17
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|