Solved

Run cmd in background

Posted on 2010-08-22
5
385 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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