Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run cmd in background

Posted on 2010-08-22
5
Medium Priority
?
420 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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…

705 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