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")
LVL 35
YZlatAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
YZlatAuthor Commented:
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
Fernando SotoRetiredCommented:
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

YZlatAuthor Commented:
oops, sorry about that...

do you know any other way to open Windows explorer so that it'd be possible to close it programmatically?
Fernando SotoRetiredCommented:
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
Fernando SotoRetiredCommented:
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
YZlatAuthor Commented:
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})")
Fernando SotoRetiredCommented:
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
YZlatAuthor Commented:
What do you mean by "Did you move the explorer window to a different directory? "? What different directory?
Fernando SotoRetiredCommented:
This is the problem with the code I gave you. Let say that "\\Server\path" is the directory N:\temp\Docs. In the Docs directory you have other folders. If you were to double click on one of the folders, OldDocs, Explorer will move into that directory, N:\temp\Docs\OldDocs, and show its content. Well when you click on the button, the code in the click event :

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

Will open a new Explorer window because there is no longer an Explorer window opened to that directory, and the second line in that code will close it immediately. But if there is an Explorer window opened to N:\temp\Docs, from my example, then it will bring that window to the front and the second line in the code will close the window.

Fernando

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YZlatAuthor Commented:
I tried that but what it does is opens a second window \\Server\path and does not close anything
YZlatAuthor Commented:
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
Fernando SotoRetiredCommented:
No I do not. But I will look around for some info on it.

Fernando
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.