Solved

Run cmd in background

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
Class object 2 26
Turn on intranet settings 1 36
I need help making a collection from a class in VB.Net 3 25
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 …
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:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

17 Experts available now in Live!

Get 1:1 Help Now