Multi thread - Application not responding

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, this is my first post and my knowledge of Visual Basic is still in its infancy (this is my first real attempt at app that is any use) however I am trying to develop an application to delete user profile and home directory folders.
I have the application working but the problem is with the application hanging (I understand this to be due to it running as a single thread and the main form gets ignored while the deletion is occuring) during the deletion process, I have tried
various ways to resolve this such as starting the delete procedure as a new thread but cant get it to work without the hang.
This is what I have:
Main Form with Delete button and ProgressBar
Clicking Delete results in a Sub DeleteUser() being called that starts the delete process and the ProgressBar
Within this Sub I have tried both calling two additional subs, hightlighed below in bold either by a new thread or just subs within this one, it works both ways in so much as it deletes the files and folders (except if there is
a permission error but thats a different issue) but both ways hang the main form with the Progress Bar. The only difference between the two is if I call both as new thread the ProgressBar completes before the deletion - is this something to do with cross threading?
Also please ingnore the ProgressBar Incrementals as they are all over the place at the minute with me moving bots of code around trying to figure this out.
Any help would be very much appreciated.
Many thanks<br/>
Rob
<div style="color:black; background-color:white
<pre><span style="color:blue Module DeleteUserSub

<span style="color:green Subroutine to delete user account, mailbox, home and profile folder
<span style="color:blue Sub DeleteUser()

<span style="color:green Start Timer on Progress Bar
MainForm.Timer1.Start()
MainForm.LabelProgress.Text = (<span style="color:#a31515 "Deleting...please wait")

<span style="color:green Create DeleteLog
DeleteLog = (<span style="color:#a31515 "C:DeleteUserLogsDelete" & strUsername & <span style="color:#a31515 ".log")

<span style="color:green Log Profile paths
StWr(<span style="color:#a31515 "ACCOUNT INFORMATION")
StWr(<span style="color:#a31515 "**************************")
StWr(<span style="color:#a31515 "INFORMATION: AD Profile Path attribute : " & strProfilePath)


<span style="color:green Determine Home Folder
strSitecode = Mid(strHomeDirectory, 3, 3)

<span style="color:blue Select <span style="color:blue Case UCase(strSitecode)
<span style="color:blue Case <span style="color:#a31515 "svr1"
strHomeFolderPath = <span style="color:#a31515 "\server1f$users" & strUsername
StWr(<span style="color:#a31515 "INFORMATION: Home Folder determined as " & strHomeFolderPath)
<span style="color:blue Case <span style="color:#a31515 "svr2"
strHomeFolderPath = <span style="color:#a31515 "\server2f$users" & strUsername
StWr(<span style="color:#a31515 "INFORMATION: Home Folder determined as " & strHomeFolderPath)
<span style="color:blue End <span style="color:blue Select

<span style="color:green Comments to Log
StWr(<span style="color:#a31515 "INFORMATION: Mailbox: " & strHomeMDB)
StWr(<span style="color:#a31515 "")
StWr(<span style="color:#a31515 "INFORMATION: Attempting to delete Profile folder........")
StWr(<span style="color:#a31515 "INFORMATION: Attempting to delete Home Folder........")
StWr(<span style="color:#a31515 "INFORMATION: Attempting to delete Mailbox........")
StWr(<span style="color:#a31515 "INFORMATION: Attempting to delete AD Account........")
StWr(<span style="color:#a31515 "")

<span style="color:green Set scripting objects
objFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")
objNetwork = CreateObject(<span style="color:#a31515 "WScript.Network")
<span style="color:green objShell = CreateObject("WScript.Shell")

<span style="color:green Initialise ErrorNum as Zero
ErrorNum = 0


<span style="color:green ***************PROFILE DELETION**************

<span style="color:green Progress Bar Increment (5)
MainForm.ProgressBar1.Increment(5)

<span style="color:green Progress Bar Increment (Total = 10)
MainForm.ProgressBar1.Increment(5)

<span style="color:green Start DeleteUserThread by calling DeleteUser sub
<span style="color:green DeleteProfileThread.Start()

<span style="color:green Call DeleteProfileSub to delete profile
DeleteProfile()

<span style="color:green Profile Delete completed - Progress Bar Increment (Total = 30)
MainForm.ProgressBar1.Increment(20)



<span style="color:green ***************HOMEDIRECTORY DELETION**************

<span style="color:green Progress Bar Increment (Total = 35)
MainForm.ProgressBar1.Increment(5)


<span style="color:green Progress Bar Increment (Total = 40)
MainForm.ProgressBar1.Increment(5)

<span style="color:green Start DeleteUserThread by calling DeleteUser sub
<span style="color:green DeleteHomeDirectoryThread.Start()

<span style="color:green Call HomeDirectorySub to delete HomeDirectory
DeleteHomeDirectory()

<span style="color:green HomeDirectory deleted - Progress Bar Increment (Total = 60)
MainForm.ProgressBar1.Increment(20)

<span style="color:green HomeDir Share removed - Progress Bar Increment (Total = 70)
MainForm.ProgressBar1.Increment(10)



<span style="color:green *****************MAILBOX DELETION**********************
<span style="color:blue If strHomeMDB <span style="color:blue Is <span style="color:blue Nothing <span style="color:blue Then
StWr(<span style="color:#a31515 "ERROR: No mailbox found")
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:green Progress Bar Increment (Total = 85)
MainForm.ProgressBar1.Increment(10)

<span style="color:blue Else

<span style="color:green Progress Bar Increment (Total = 75)
MainForm.ProgressBar1.Increment(5)

<span style="color:green Attempt to delete mailbox
<span style="color:blue Try
objUser.DeleteMailbox()
objUser.SetInfo()
StWr(<span style="color:#a31515 "COMPLETED: Mailbox deleted")
StWr(<span style="color:#a31515 "")

<span style="color:green Progress Bar Increment (Total = 85)
MainForm.ProgressBar1.Increment(10)

<span style="color:blue Catch Ex <span style="color:blue As Exception
StWr(<span style="color:#a31515 "ERROR: Unable to delete mailbox")
StWr(<span style="color:#a31515 "")
StWr(Ex.Message)

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:green Progress Bar Increment (Total = 85)
MainForm.ProgressBar1.Increment(10)
<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue If


<span style="color:green *********************ACTIVE DIRECTORY ACCOUNT DELETION**********************

<span style="color:green Progress Bar Increment (Total = 90)
MainForm.ProgressBar1.Increment(5)


<span style="color:green Attempt to delete AD account
<span style="color:blue Try
objContainer = GetObject(objUser.Parent)
objContainer.Delete(<span style="color:#a31515 "user", <span style="color:#a31515 "cn=" & strCn)
StWr(<span style="color:#a31515 "COMPLETED: AD account deleted")

<span style="color:green Progress Bar Increment (Total = 100)
MainForm.ProgressBar1.Increment(5)

<span style="color:blue Catch ex <span style="color:blue As Exception
StWr(<span style="color:#a31515 "ERROR: Unable to delete AD account")
StWr(<span style="color:#a31515 "")
StWr(ex.Message)

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:green Progress Bar Increment (Total = 100)
MainForm.ProgressBar1.Increment(5)

<span style="color:blue End <span style="color:blue Try


<span style="color:green Shows ErrorForm if errors have occured
<span style="color:blue If ErrorNum <> 0 <span style="color:blue Then
ErrorForm.Show()
StWr(ErrorNum)
<span style="color:blue End <span style="color:blue If


MainForm.LabelProgress.Text = (<span style="color:#a31515 "User Deleted")
<span style="color:blue End <span style="color:blue Sub

<span style="color:blue End <span style="color:blue Module

*******************************************************************************************************

<span style="color:blue Module DeleteProfileSub

<span style="color:blue Sub DeleteProfile()
StWr(<span style="color:#a31515 "ACCOUNT DELETION")
StWr(<span style="color:#a31515 "********************")

<span style="color:green Check if Profile Directory exists
<span style="color:blue If <span style="color:blue My.Computer.FileSystem.DirectoryExists(strProfilePath) <span style="color:blue Then

<span style="color:green AD ProfilePath is only subfolder (xp), parent folder (userid)
<span style="color:green that needs to be deleted. Hence:

<span style="color:green Get Parent folder of strProfilePath returned from AD
strProfilePathParentPath = <span style="color:blue My.Computer.FileSystem.GetParentPath(strProfilePath)


<span style="color:green Attmept to delete Profile Directory
<span style="color:blue Try
<span style="color:green First Set Attributes as exception occurs if set to Read Only
<span style="color:green SetAttributes(strProfilePath)

<span style="color:green My.Computer.FileSystem.DeleteDirectory(strProfilePathParentPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
objFSO.DeleteFolder(strProfilePathParentPath, <span style="color:blue True)
StWr(<span style="color:#a31515 "COMPLETED: The profile folder " & strProfilePath & <span style="color:#a31515 " has been deleted.")
StWr(<span style="color:#a31515 "")

<span style="color:green Catch access permission error
<span style="color:blue Catch IOEx <span style="color:blue As IOException
StWr(<span style="color:#a31515 "ERROR: Permission to delete folder denied, save log and manually take ownership to delete folder")
StWr(IOEx.Message)
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:green Catch any other error other than permissions on first delete attempt
<span style="color:blue Catch Ex <span style="color:blue As Exception
StWr(<span style="color:#a31515 "ERROR: An error occured")
StWr(Ex.Message)
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1
<span style="color:blue End <span style="color:blue Try


<span style="color:blue Else
StWr(<span style="color:#a31515 "ERROR: The profile folder for user " & strUsername & <span style="color:#a31515 " does not exist")
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue Sub

<span style="color:blue End <span style="color:blue Module

*******************************************************************************************************

<span style="color:blue Module DeleteHomeDirectorySub
<span style="color:blue Sub DeleteHomeDirectory()

<span style="color:green Check if Home directory exists
<span style="color:blue If <span style="color:blue My.Computer.FileSystem.DirectoryExists(strHomeFolderPath) <span style="color:blue Then

<span style="color:green First Set Attributes as exception occurs if set to Read Only
<span style="color:green SetAttributes(strHomeFolderPath)


<span style="color:green Try to delete Home Directory
<span style="color:blue Try
<span style="color:green My.Computer.FileSystem.DeleteDirectory(strHomeFolderPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
objFSO.DeleteFolder(strHomeFolderPath, <span style="color:blue True)
StWr(<span style="color:#a31515 "COMPLETED: The home folder " & strHomeFolderPath & <span style="color:#a31515 " has been deleted")
StWr(<span style="color:#a31515 "")
<span style="color:blue Catch IOEx <span style="color:blue As IOException
StWr(<span style="color:#a31515 "ERROR: Permission to delete folder denied")
StWr(IOEx.Message)
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1
<span style="color:blue Catch Ex <span style="color:blue As Exception
StWr(<span style="color:#a31515 "ERROR: An error occured")
StWr(Ex.Message)
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:blue End <span style="color:blue Try


<span style="color:blue Else
StWr(<span style="color:#a31515 "ERROR: The home folder for user " & strUsername & <span style="color:#a31515 " does not exist")
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:blue End <span style="color:blue If

<span style="color:green Disable the user share using RMTSHARE from CMD
<span style="color:green NOTE: Rmtshare has to be in the folder specified below
ProcessStartInfo = <span style="color:blue New System.Diagnostics.ProcessStartInfo()
ProcessStartInfo.FileName = <span style="color:#a31515 "cmd.exe"

ProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
ProcessStartInfo.Arguments = <span style="color:#a31515 "/k C:DeleteUserResourcesRmtShare.exe " & strHomeDirectory & <span style="color:#a31515 " /DELETE"

<span style="color:blue Try
Process = System.Diagnostics.Process.Start(ProcessStartInfo)
StWr(<span style="color:#a31515 "COMPLETED: Share " & strHomeDirectory & <span style="color:#a31515 " has been removed")
StWr(<span style="color:#a31515 "")

<span style="color:blue Catch Ex <span style="color:blue As Exception
StWr(<span style="color:#a31515 "ERROR: Unable to unshare " & strHomeDirectory)
StWr(<span style="color:#a31515 "")

<span style="color:green Increment ErrorNum
ErrorNum = ErrorNum + 1

<span style="color:blue End <span style="color:blue Try
<span style="color:blue End <span style="color:blue Sub

<span style="color:blue End <span style="color:blue Module
[/code]

<br/>


View the full article
 
Back
Top