Solved

Run cmd in background

Posted on 2010-08-22
5
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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