Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

How to stop a process?

I run a process using Process.Start. I wnat to stop that process when application becomes idle or when user clicks END button.
The process I run is simply open a directory on the network:

Process.Start("\\Server\path")
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi YZlat;

Tyr it like this.

Place a class level variable like this in the form code.

        Private myProcess As Process

When you start the process do it like this.

        myProcess = Process.Start("\\Server\path")

When you want to stop the process then do this.

        If Not myProcess.HasExited Then
            myProcess.Kill()
        End If


Fernando
Avatar of YZlat

ASKER

Bob, I get an error Object reference is not set to an instance of an object in line   If Not myProcess.HasExited Then


Even after executing line

myProcess = Process.Start("\\Server\path")


myProcess remains null
It's Fernando; I did not realize that you were opening a Window Explorer window up. The most likely reason it did not return a Process for that is that no new process was stated. When we open a Explorer window it is a child process of explorer.exe and is always running on the system. Do not know how to stop a child process of explorer.

Fernando
Avatar of YZlat

ASKER

oops, sorry about that...

do you know any other way to open Windows explorer so that it'd be possible to close it programmatically?
Hi YZlat;

I did come up with a solution to your problem. Open the Windows Explorer window as you did before.

        Process.Start("C:Datafiles")

The when you want to close the window do the following.

        Process.Start("C:Datafiles")
        SendKeys.Send("%({F4})")

Now let me explain what is happening here. When you call the window to start a second time it does not start or open a new window but brings the currently opened window to the front. Once the window is in the front of all the other windows you can send that application a Alt+F4 key combo which will close the active window.

Fernando
I re-posted because the path was incorrect.

I did come up with a solution to your problem. Open the Windows Explorer window as you did before.

        Process.Start("\\Server\path")

The when you want to close the window do the following.

        Process.Start("\\Server\path")
        SendKeys.Send("%({F4})")

Now let me explain what is happening here. When you call the window to start a second time it does not start or open a new window but brings the currently opened window to the front. Once the window is in the front of all the other windows you can send that application a Alt+F4 key combo which will close the active window.

Fernando
Avatar of YZlat

ASKER

Fernando, unfortunatelly it didn't work - it opened a new window and did not close it

I put this code inside a click event of a button:
Process.Start("\\Server\path")
        SendKeys.Send("%({F4})")
Hi YZlat;

Did you move the explorer window to a different directory? This is the only reason I can see that would cause this. But in that case it should have open a new Explorer window at the directory \\Server\path and then immediately close it.

Fernando
Avatar of YZlat

ASKER

What do you mean by "Did you move the explorer window to a different directory? "? What different directory?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

I tried that but what it does is opens a second window \\Server\path and does not close anything
Avatar of YZlat

ASKER

It worked after I declared a global string sPath and used sPath variable instead of "\\Server\path" string in both Process.Start

Thanks!

The only problem is that whenever you open a pth on the network, it creates a pipe on your machine and when folder is closed, the pipe is not destroyed. But if you destroy the pipe, the folder shuts itself down too

Do you know how to close the pipe programmatically? If you do, I will open another question for you
No I do not. But I will look around for some info on it.

Fernando