Solved

Run cmd in background

Posted on 2010-08-22
5
383 Views
Last Modified: 2013-11-07
I have this code, but I am not sure how to set it up to run the command in the background.  What do I do with the dll?  Is this something I need to add a referance too?  Also I do I call this from a button?
0
Comment
Question by:chadmanvb
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33495597
code?
0
 

Author Comment

by:chadmanvb
ID: 33495608
Opps, here it is

<DllImport("user32.dll")> _
Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
End Function

Private Shared Sub AddUserToGroup(userDn As String, groupDn As String)
      Dim exitCode As Integer = StartBackgroundProcess("dsmod", String.Format("group ""{0}"" -addmbr ""{1}""", groupDn, userDn))
      If exitCode <> 0 Then
            Console.WriteLine("Could not remove user {0} from group {1}", userDn, groupDn)
      End If
End Sub

Private Shared Sub RemoveUserFromGroup(userDn As String, groupDn As String)
      Dim exitCode As Integer = StartBackgroundProcess("dsmod", String.Format("group ""{0}"" -rmmbr ""{1}""", groupDn, userDn))
      If exitCode <> 0 Then
            Console.WriteLine("Could not remove user {0} from group {1}", userDn, groupDn)
      End If
End Sub

Private Shared Function StartBackgroundProcess(procPath As String, cmdLine As String) As Integer
      Dim psi As New ProcessStartInfo(procPath, cmdLine)
      psi.WindowStyle = ProcessWindowStyle.Hidden
      psi.CreateNoWindow = True
      psi.UseShellExecute = False

      Dim proc As Process = Process.Start(psi)
      ShowWindow(proc.MainWindowHandle, 0)
      proc.WaitForExit()
      Return proc.ExitCode
End Function
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33495609
you simply run the posted function and it will hide the console in the background.
nothing else need to be done.
for the sake of other experts, the question relate to how to run console command in the background.
Private Shared Function StartBackgroundProcess(procPath As String, cmdLine As String) As Integer
	Dim psi As New ProcessStartInfo(procPath, cmdLine)
	psi.WindowStyle = ProcessWindowStyle.Hidden
	psi.CreateNoWindow = True
	psi.UseShellExecute = False

	Dim proc As Process = Process.Start(psi)
	ShowWindow(proc.MainWindowHandle, 0)
	proc.WaitForExit()
	Return proc.ExitCode
End Function

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33495627
u need to add a reference to System.Runtime.InteropServicesother than than in the button click event handler, add the code which runs the console command.for example:dim exitCode as Int = StartBackgroundProcess("iisrest", "/stop")
0
 

Author Closing Comment

by:chadmanvb
ID: 33495629
Got and it works great!  I was not sure what to do the dll line.  I just left it out and it works.  Thanks so much for all the help today!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now