[vb 2005 expr] Killing a copy-dir process

Hi.
In my VB 2005 express application I make a copy of one remote directory (connected as a net unit) to my local hard disk with this line:

My.Computer.FileSystem.CopyDirectory("F:\namedir1", "C:\namedir1",True)

This works inside a backgroundworker thread.

When I cancel the thread, the copy still goes on, especially if the source-directory is big (some Gb).
I want to stop immediately the copy, how can I do this?
Is there a way to know what process ID is assigned to that command and to kill it?
How to kill aMy.Computer.FileSystem.CopyDirectory command?

The killing will be inside the background_cancel event.

Moreover I noticed that even if I cancel my threads and close my application I still see 2 processes in my task manager with the name of my application (mybackup.exe) ... why?
Thanks in advance for any hint.SmileSmile

[1190 byte] By [Quisni] at [2007-12-17]
# 1
any idea?Sad
Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Probably it's a OS call that can't really be stopped by normal thread handling operations.

Now, I'm not sure if what I'm about to propose is a good idea - killing a copy process half point may be asking for hard disk corruption. Truth is, I just don't know if it's a good idea or not, or even if it works.

But, if you create a simple executable that does the copy and invoke it from your app through system.Diagnostics.Process then you should be able to kill it and stop the copy immediately.

Good luck.

AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Thank you.
And what if I want to kill a process in my task manager?
My application name is
BackupAll.exe
and it is, obviously, in my tsk manager when it runs.
When I close it, while it is burning a dvd (using nero com object), the form closes but in task manager BackupAll.exe still exists!!
How can I kill it when I close my application?
Is there a simple and effective way?
Thanks!!Smile
Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Again, I don't know if that is a very good idea - I'd say there's a good chance your dvd will become unusable, but System.Diagnostics.Process has a couple of methods you can use - GetProcessesByName() if you know the process's name, and GetProcessById if you know it's id - once you get the process object, you should be able to use the kill method to stop the process.
AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
This may work, although i havnt had a chance to test this code...
Under your Backgroundoworker do work process add this code (assumes your backgroundworker is called backgroundworker1.

Then make a button called cancel..add this onclick

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

BackgroundWorker1.CancelAsync()


Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'DO YOUR COPY CODE HERE
If
BackgroundWorker1.CancellationPending = True Then

me.close

End If
end sub

Again I dont know if this will work in your situation as im not sure if me.close will force your app to close since you have a big file transfer going. but its worth a try.

Mjman15 at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
I'll give it a try.
Thanks.
Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
Well, I know that it isn't a good idea and that dvd surely will be unusable, but all I want in this moment is to stop (really STOP) my application when user clicks on the close button (the X on top right) of the main form. That's all.
I'll try GetProcessesByName(), since I know that my process is MyBackup.exe... but then how can I kill it?
Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8
Get processes by name returns a list of process class objects that represents all the processes that have that particular name - assuming you only start one, you'll only have one element, and you can call the kill() method of that object to stop it, or go through the whole list and kill them all.
AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9
I've tried this way:



' Get all instances of Backup_dvd.exe running on the local computer.
Dim localByName As Process() = Process.GetProcessesByName("Backup_dvd.exe")
Dim p As Integer
If localByName.Length > 0 Then
For p = 0 To localByName.Length - 1
localByName(p).Close()
localByName(p).Kill()
Next p
End If

but it seems NOT to work... Backup_dvd.exe remains in my task manager when I close my application (only if I started a burn process before closing).

Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 10

I was taking a look at the process name and is seems that it doe not include the extension - try with "Backup_dvd" instead of "Backup_dvd.exe"

Also, that Close() method doesn't seem to do what you're thinking - I'd leave it out, or use CloseMainWindow() - note that if CloseMainWindow() suceeds, the kill method may fail since the process is now gone.

AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 11
The "close" method releases all resources, while "kill" method destroys the process (this is what I understand from help).
*********
What about if I try with the processID?
What should I do to get my application current PID?
How can I destroy (kill) a process using its PID?

ThanksSmile

Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 12
For the close method, I think it releases all resources of the object that represents the process, not the process itself - I think you're supposed to use it if you don't plan to use the managed object anymore, but it won't affect the process itself.

The code you had posted before works, but you need to use the process name without it's extension, and not use the .Close method.
To answer your other questions:

You can get the executing process with:

system.Diagnostics.Process.GetCurrentProcess.Id

You can get a particular process by it's id by using:

System.Diagnostics.Process.GetProcessById()

and you can kill it by just calling the Kill method on the returned process.

AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 13

Thank you very much.
I solved my problem with these lines:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As_
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
nero.Abort()

Me.BackgroundWorker1.CancelAsync()
Me.BackgroundWorker2.CancelAsync()

'deleting definitevely nero com object
Dim y As Integer
y = System.Runtime.InteropServices.Marshal.ReleaseComObject(nero)
Do Until y = 0
y = System.Runtime.InteropServices.Marshal.ReleaseComObject(nero)
Loop

End Sub

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As_

System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
System.Diagnostics.Process.GetCurrentProcess.CloseMainWindow()
System.Diagnostics.Process.GetCurrentProcess.Close()
System.Diagnostics.Process.GetCurrentProcess.Kill()
End Sub


Now when I close the main window-form (the only one!) all resources are released e process disappears from task manager!
Thank you everybody.
If you have some comments about my solution please let me know!

Quisni at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 14
Quisni, would you do me a favor and try something? Place a msgbox call after System.Diagnostics.Process.GetCurrentProcess.CloseMainWindow() - I've got a feeling that the other two calls never happen.
AlexMoura at 2007-10-6 > top of Msdn Tech,Visual Basic,Visual Basic Language...