[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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")
0
YZlat
Asked:
YZlat
  • 7
  • 6
1 Solution
 
Fernando SotoCommented:
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
0
 
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
0
 
Fernando SotoCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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?
0
 
Fernando SotoCommented:
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
0
 
Fernando SotoCommented:
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
0
 
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})")
0
 
Fernando SotoCommented:
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
0
 
YZlatAuthor Commented:
What do you mean by "Did you move the explorer window to a different directory? "? What different directory?
0
 
Fernando SotoCommented:
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
0
 
YZlatAuthor Commented:
I tried that but what it does is opens a second window \\Server\path and does not close anything
0
 
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
0
 
Fernando SotoCommented:
No I do not. But I will look around for some info on it.

Fernando
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now