[RESOLVED] [2005] Process questions
Okay, I have a few questions about processes.
First question, how would I start a process like text.exe and run commands to it like:
C:\test.exe -command1 -command2
Second question, how can I set a process's affinity using code. For now the app is just running on a dual core, so Core 0 and Core 1.
Third question, I know my application will end up on machines with more than 2 cores, so how can I list all processor cores?
Re: [2005] Process questions
Start a process with command arguments like this:
VB Code:
Process.Start("C:\test.exe","-command1 -command2")
I know you can get the number of processor cores with the performancecounter, one second..
Re: [2005] Process questions
Never mind the performancecounter, i found a key in the registry that was more useful. This code will give you the number of cores:
VB Code:
Dim coreCount As Integer = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor").GetSubKeyNames.Length
MessageBox.Show(CStr(coreCount))
Re: [2005] Process questions
But then your app would need permissions to the registry key outside of the HKCU.
You could just use WMI to get the processor information.
Re: [2005] Process questions
How would I terminate that specific process that I started?
Re: [2005] Process questions
Quote:
Originally Posted by tylerm
How would I terminate that specific process that I started?
You can use the Kill method. :bigyello:
http://msdn2.microsoft.com/en-us/lib...cess.kill.aspx
Re: [2005] Process questions
Yeah like nmadd said, the Kill method is the way to kill your process. If you however, would like to try to close it more "gently" this would be another option:
VB Code:
Dim p As Process = Process.Start("C:\test.exe", "-command1 -command2")
'Calling CloseMainWindow is like pressing the X on the window, it will prompt the user to save his/her work if needed.
p.CloseMainWindow()
'Wait 5 seconds for the process to close
p.WaitForExit(5000)
'If it still hasnt closed, kill it.
If Not p.HasExited Then
p.Kill()
End If
Re: [2005] Process questions
Good stuff Atheist. I should probably try to be more gentle with my Processes. :bigyello:
Re: [2005] Process questions
Yes, kill it abrupt in its process termination (is like clicking "Terminate Process" on the task manager) so anything you can do to allow the process to end properly and dispose of its own resources is best. :)
Re: [2005] Process questions
Would the procedures still be the same in this scenario:
The application will start multiple instnaces of test.exe, each with different command attirbutes. I have a listview control that lists each setup, ie: C:\test.exe -command SETTING1 -variable SETTING1
And for setup 2:
C:\test.exe -command SETTING2 - variable SETTING2
How could I make it so they select an item in the listview and click a button, and they instance of test.exe is killed?
I'm a little confused since the method Im trying is like this:
2 buttons and a listview.
buttonStart:
Dim pTestExe As Process
pTestExe.Start(ListView1.FocusedItem.Text, "-command -command -command")
buttonStop:
?
Since the pTestExe process is not the same exact process everytime, I think there might be problems.
Re: [2005] Process questions
You will probably have multiple instances of the test.exe program in the running task list. Each will have a separate process ID number. When starting the process you may want to store it in the listitems tag property or create a hidden column for it in the listview. Thi swill help when you go to terminate the process you will be able to verify its the desired instance of test.exe by matching the process ids :)
Re: [2005] Process questions
Okay, so now I'm stuck on setting affinity.
I tried defining an Int16, didn't work, I've tried binary (Couldn't find a property for it).
Here was my last try:
VB Code:
Dim cpu0 As Int16 = "0x0001"
srcIN.ProcessorAffinity = cpu0
Re: [2005] Process questions
Re: [2005] Process questions
I have no experience with this but the documentation says that this property wants an IntPtr.
http://msdn2.microsoft.com/en-us/lib...ty(vs.80).aspx
Something like
vb.net Code:
p.ProcessorAffinity = New IntPtr(1)
perhaps?
Re: [2005] Process questions
Perfect. I can't believe I didn't see that.
Also for future reference. Core 0 = 1 and Core 1 = 2