Im trying to build a cmd.exe wrapper but i cant figure out how to wait for a process inside the cmd process to finish.
When im run ping.exe in my program, my "inputline" (the path) will output to the console before the ping-command is finished.
I cant use: .WaitForInputIdle(); because I don't have a GUI. "This could be because the process does not have a graphical interface."
Is it even possible to solve or is there any better way to do it?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ObjectTest
{
class cmd
{
Process cmdProcess;
StreamWriter cmdStreamWriter;
public cmd()
{
//WinAPI.ShowConsoleWindow();
Process();
//WinAPI.HideConsoleWindow();
}
private void Process()
{
StartCmd();
string command = "";
while (command != "exit")
{
Thread.Sleep(100);
cmdProcess.WaitForInputIdle();
Console.Write(Environment.NewLine + Directory.GetCurrentDirectory() + "> ");
command = Console.ReadLine();
SendCommand(command);
}
}
private void StartCmd()
{
cmdProcess = new Process();
cmdProcess.StartInfo.FileName = "cmd.exe";
cmdProcess.StartInfo.UseShellExecute = false;
cmdProcess.StartInfo.CreateNoWindow = true;
cmdProcess.StartInfo.RedirectStandardOutput = true;
cmdProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
cmdProcess.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
cmdProcess.StartInfo.RedirectStandardInput = true;
cmdProcess.Start();
cmdStreamWriter = cmdProcess.StandardInput;
cmdProcess.BeginOutputReadLine();
}
private void SendCommand(string command)
{
cmdStreamWriter.WriteLine(command);
}
private void btnQuit_Click(object sender, EventArgs e)
{
cmdStreamWriter.Close();
cmdProcess.WaitForExit();
cmdProcess.Close();
}
private static void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!string.IsNullOrEmpty(outLine.Data))
{
Console.WriteLine(outLine.Data);
}
}
}
}
If it's only actual processes you want to catch you can use polling methods as such:
Before running the command, collect all existing processes of, for example, ping (.exe), using Process.GetProcessesByName, and store their PIDs.
Run the command
In order to wait for exit, Again scan for all ping processes, and wait on all processes that did not previously exist.
Note that this will fail, or at least not be accurate, if another process will launch another ping between steps 1 and 3.
You should also keep in mind that CMD does not wait on all type of processes, only those which are defined as console applications (see this question).
When starting a process, you can specify that you want to wait:
var process = Process.Start(...);
process.WaitForExit();
Here's a link to MSDN explaining it: http://msdn.microsoft.com/en-us/library/fb4aw7b8(v=vs.110).aspx
I think this is wat you need.
Related
I have an assignment in C# involving automation. My code is mainly based off things I have found online. My current problem is I am trying to read in IP addresses line by line from a CSV file into an array, pinging each object of the array, and appending the object so that each object will read "IP, status(if the ping was successful), response time"
I am trying to use a foreach loop but am running into errors. I am on a Mac so it is using mono. Any help would be appreciated.
(Full assignment below if it helps explain anything)
Starting with a premade C# console application, your application will upon running allow for three commands – start, stop, and exit. The application will, upon first running, ask the user for a CSV file (the user must provide a CSV file). This file will contain web addresses (1 per line) – the professor will supply the initial file.
Your application will be setup with a timer so that every time the timer runs, it pings the provided web addresses, gathering whether the server responds or not and the response time of each server. The timer should run continuously from the point the user types “start” until they type “stop” or “exit”. The data will be recorded back to another CSV file, containing the web address, response time, and whether or not the server responded at all.
using System;
using System.Timers;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using Microsoft.VisualBasic;
namespace MT_proj4
{
public class Program
{
public static String[] addressArray = File.ReadAllLines("Project04_URLs.csv");
public static string IP;
public bool status;
public string time;
public static void Main()
{
foreach (string s in addressArray)
{
IP = Console.ReadLine();
bool PingHost(string)
{
bool pingable = false;
#pragma warning disable XS0001 // Find APIs marked as TODO in Mono
Ping pinger = new Ping();
#pragma warning restore XS0001 // Find APIs marked as TODO in Mono
try
{
PingReply reply = pinger.Send(IP);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
return false;
}
for (int i = 0; i < addressArray.Length; i++)
{
addressArray[i] = addressArray[i] + pingable;
}
}
}
}
static void TimerClass()
{
Timer timer = new Timer(5000);
timer.Elapsed += HandleTimer;
timer.Start();
Console.WriteLine("Type 'exit' to close...");
Console.WriteLine("Enter a command...");
while (true)
{
Console.Write("Command: ");
string command = Console.ReadLine();
Console.WriteLine("Command entered: " + command);
if (command.ToLower() == "stop")
timer.Stop();
else if (command.ToLower() == "exit")
break;
}
}
static void HandleTimer(Object source, ElapsedEventArgs e)
{
Console.WriteLine("\nHandler not implemented...");
}
}
I would restructure your solution a bit. The ping code should run its own thread. Remove the timer, and simply block on Console.ReadLine(). If the results are not expected, repeat until you get either stop or exit.
The reason is the ping work and the console processing are on the same thread. These will block.
Try this:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("pinging....until stop command");
Thread pinger = new Thread(DoPinging);
pinger.Start();
while (true)
{
string command = Console.ReadLine();
if (command.ToLower() == "stop")
break;
else if (command.ToLower() == "exit")
break;
Console.WriteLine("Ignoring-> " + command);
}
pinger.Abort();
}
static void DoPinging()
{
// here goes the pinging code
}
}
In your example code, it looks like you want to read input for ping from Console as well, after the line read, if the value doesn't equal the stop commands (stop, exit), put the value into a queue and let the ping code read from the queue.
You mention reading a CSV file for the addresses to ping (but your example code doesn't reference a file). You can take this as a command line argument (string[] passed in main) and do the work for processing the file in the DoPinging method.
If you want me to add more detail to that, please comment below and I will work on that.
Thnx
Matt
I am trying simple windows service it works fine till there is no connection with database.once I establish connection my service get installed and start successfully but does not work correctly and does not get stop.It throws an Error as :"Windows Could not Stop service on local computer".
Following is Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Timers;
using System.Data;
using System.Data.SqlClient;
namespace tgfservice4
{
public partial class tgfservice4 : ServiceBase
{
private static string con = "Data Source=ABC;Initial Catalog=ABC;User Id=ABC;Password=ABC";//ConfigurationManager.ConnectionStrings["ABCD"].ToString();
private SqlConnection sn = new SqlConnection(con);
private SqlCommand sm;
Timer timer = new Timer();
public tgfservice4()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//add this line to text file during start of service
TraceService("start service");
//handle Elapsed event
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
//This statement is used to set interval to 1 minute (= 60,000 milliseconds)
timer.Interval = 60000;
//enabling the timer
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
TraceService("stopping service");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
TraceService("Another entry at " + DateTime.Now);
}
private void TraceService(string content)
{
sn.Open();
// sm = new SqlCommand("Update_Table", sn);
// sm.CommandType = CommandType.StoredProcedure;
try
{
// sm.Parameters.AddWithValue("#value", "0");
// sm.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
sm.Dispose();
sn.Close();
sn.Dispose();
}
//set up a filestream
FileStream fs = new FileStream(#"d:\MeghaService.txt", FileMode.OpenOrCreate, FileAccess.Write);
//set up a streamwriter for adding text
StreamWriter sw = new StreamWriter(fs);
//find the end of the underlying filestream
sw.BaseStream.Seek(0, SeekOrigin.End);
//add the text
sw.WriteLine(content);
//add the text to the underlying filestream
sw.Flush();
//close the writer
sw.Close();
}
}
}
I think your OnStop method is taking long time to connect to database and opearting on that connection.
Generally you are supposed to do heavy operation in another thread in background.
Usually the OnStart as well as OnStop events within the windows service are used to initiate a process and the time consuming process will carryout it's execution within a child thread
If there is any error on startup then also service may behave like this. To debut you can do something like following.
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
...
}
and then attach visual studio to the process and debug the issue.
also you can look into the Event viewer for system and application logs.
computer/server manager -> Event Viewer -> Windows Logs -> Application
If an exception is thrown in OnStop, the Service Control Manager will not unload your service. As such, in case you have a problem connecting to the database, just log this in the event log without throwing further errors. Also, do not attempt to close the connection if it is not already opened. Add a try catch upon closing the connection. Bottom line, make sure nothing blows up in the OnClose.
I have got a problem with my C# project. I got there 2 applications:
Executor application which I will call Mini Launcher
Executed application which I will call Launcher
My problem is: I want to run my Launcher by Mini launcher and in on Show event of Launcher app close Mini Launcher.
My Mini Launcher is something like splash screen but with additional functionality like upgrade Launcher, and other. Its my execution code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = "My Directory"
startInfo.FileName = "My App";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.();
}
}
catch
{
...
}
Have a look at the Mutex class. Named mutices provide a way for applications to send signals to one another.
The following sample shows two Console-applications. The TestMutexLauncher-application launches the TestMutex application:
using System;
using System.Diagnostics;
using System.Threading;
namespace TestMutexLauncher
{
class Program
{
static void Main(string[] args)
{
var p = Process.Start("TestMutex");
Console.WriteLine("Waiting for other process to release the mutex.");
Thread.Sleep(1000); // maybe p.WaitForInputIdle is an alternative for WinForms/WPF
Mutex mutex = null;
for (int i = 0; i < 100; i++)
{
if (Mutex.TryOpenExisting("MyUniqueMutexName", out mutex))
break;
Thread.Sleep(100);
}
if (mutex != null)
{
try
{
mutex.WaitOne();
mutex.ReleaseMutex();
}
finally
{
mutex.Dispose();
}
}
}
}
}
The launcher application starts the process and waits for a Mutex to be created in the other process. If it can acquire ownership of the the Mutex in a specified time frame, it waits to get ownership of the Mutex. After that, it realeases and disposes the Mutex.
The first task of the launched application is to create the Mutex, do the initialization actions and then release the Mutex.
using System;
using System.Threading;
namespace TestMutex
{
class Program
{
static void Main(string[] args)
{
using (var mutex = new Mutex(true, "MyUniqueMutexName"))
{
// Do something
for (int i = 0; i < 10000; i++)
Console.Write(".");
Console.WriteLine();
Console.WriteLine("Press enter...");
Console.ReadLine();
mutex.ReleaseMutex();
}
for (int i = 0; i < 10000; i++)
Console.Write(".");
Console.WriteLine();
Console.WriteLine("Press enter...");
Console.ReadLine();
}
}
}
Firstly, I would recommend you consider:
1) Do they actually need to be separate applications?
2) If so, why can't MiniLauncher just close itself after Launcher has loaded?
But if you have to do it this way, then the code you're looking for is something like this:
private void OnShow()
{
var target = Process.GetProcessesByName("MiniLauncher.exe").FirstOrDefault();
if (target != null)
{
// any other checks that this is indeed the process you're looking for
target.Close();
}
}
you can call another project executable from current running project and then you can close your application.
class Program
{
static void Main()
{
//
// Open the application "application" that is in the same directory as
// your .exe file you are running.
//
Process.Start("example.txt");
// or for another directory you need to specify full path
Process.Start("C:\\");
}
}
I have an odd problem I can't seem to diagnose.
I'm calling am external binary, waiting for it to complete and then passing back a result based on standard out. I also want to log error out.
I wrote this and it works a treat in Windows 7
namespace MyApp
{
class MyClass
{
public static int TIMEOUT = 600000;
private StringBuilder netOutput = null;
private StringBuilder netError = null;
public ResultClass runProcess()
{
using (Process process = new Process())
{
process.StartInfo.FileName = ConfigurationManager.AppSettings["ExeLocation"];
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(ConfigurationManager.AppSettings["ExeLocation"]);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
netOutput = new StringBuilder();
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
netError = new StringBuilder();
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (process.WaitForExit(TIMEOUT))
{
// Process completed handle result
//return my ResultClass object
}
else
{
//timed out throw exception
}
}
}
private void NetOutputDataHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
//this is being called in Windows 7 but never in Windows Server 2008
if (!String.IsNullOrEmpty(outLine.Data))
{
netOutput.Append(outLine.Data);
}
}
private void NetErrorDataHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
//this is being called in Windows 7 but never in Windows Server 2008
if (!String.IsNullOrEmpty(outLine.Data))
{
netError.Append(outLine.Data);
}
}
}
}
So I install it on a Windows Server 2008 box and the NetOutputDataHandler and NetErrorDataHandler handlers are never called.
The app is compiled for .NET v4.
Any ideas what I'm doing wrong?
You might want to inspect the code access security policy.
You may start by runing the caspol.exe with the -l parameter(s) of the caspol.exe for the -u[ser] running the app.
Don't forget to check if you still encounter the same issue when running the app as Administrator
I'm calling Process.Start, but it blocks the current thread.
pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
// Start process
mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Trace.TraceError("Unable to run process {0}.");
}
Even when the process is closed, the code doesn't respond anymore.
But Process.Start is really supposed to block? What's going on?
(The process start correctly)
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
class Test
{
[STAThread]
public static void Main()
{
Thread ServerThread = new Thread(AccepterThread);
ServerThread.Start();
Console.WriteLine (" --- Press ENTER to stop service ---");
while (Console.Read() < 0) { Application.DoEvents(); }
Console.WriteLine("Done.");
}
public static void AccepterThread(object data)
{
bool accepted = false;
while (true) {
if (accepted == false) {
Thread hThread = new Thread(HandlerThread);
accepted = true;
hThread.Start();
} else
Thread.Sleep(100);
}
}
public static void HandlerThread(object data)
{
ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
Console.WriteLine("Starting process.");
// Start process
Process mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Console.WriteLine("Unable to run process.");
}
Console.WriteLine("Still living...");
}
}
}
Console output is:
--- Press ENTER to stop service ---
Starting process.
Found it:
[STAThread]
Makes the Process.Start blocking. I read STAThread and Multithreading, but I cannot link the concepts with Process.Start behavior.
AFAIK, STAThread is required by Windows.Form. How to workaround this problem when using Windows.Form?
News for the hell:
If I rebuild my application, the first time I run application work correctly, but if I stop debugging and restart iy again, the problem araise.
The problem is not raised when application is executed without the debugger.
No, Process.Start doesn't wait for the child process to complete... otherwise you wouldn't be able to use features like redirected I/O.
Sample console app:
using System;
using System.Diagnostics;
public class Test
{
static void Main()
{
Process p = new Process {
StartInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe")
};
p.Start();
Console.WriteLine("See, I'm still running");
}
}
This prints "See, I'm still running" with no problems on my box - what's it doing on your box?
Create a ProcessStartInfo and set UseShellExecute to false (default value is true). Your code should read:
pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
pInfo.UseShellExecute = false;
// Start process
mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Trace.TraceError("Unable to run process {0}.");
}
I had the same issue and starting the executable creating the process directly from the executable file solved the issue.
I was experiencing the same blocking behavior as the original poster in a WinForms app, so I created the console app below to simplify testing this behavior.
Jon Skeet's example uses Notepad, which only takes a few milliseconds to load normally, so a thread block may go unnoticed. I was trying to launch Excel which usually takes a lot longer.
using System;
using System.Diagnostics;
using static System.Console;
using System.Threading;
class Program {
static void Main(string[] args) {
WriteLine("About to start process...");
//Toggle which method is commented out:
//StartWithPath(); //Blocking
//StartWithInfo(); //Blocking
StartInNewThread(); //Not blocking
WriteLine("Process started!");
Read();
}
static void StartWithPath() {
Process.Start(TestPath);
}
static void StartWithInfo() {
var p = new Process { StartInfo = new ProcessStartInfo(TestPath) };
p.Start();
}
static void StartInNewThread() {
var t = new Thread(() => StartWithPath());
t.Start();
}
static string TestPath =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\\test.xlsx";
}
Calls to both StartWithPath and StartWithInfo block my thread in a console app. My console does not display "Process Started" until after the Excel splash screen closes and the main window is open.
StartInNewThread will display both messages on the console immediately, while the splash screen for Excel is still open.
We had this problem when launching a .bat script that was on a network drive on a different domain (we have dual trusted domains). I ran a remote C# debugger and sure enough Process.Start() was blocking indefinitely.
When repeating this task interactively in power shell, a security dialog was popping up:
As far as a solution, this was the direction we went. The person that did the work modified domain GPO to accomplish the trust.
Start server via command prompt:
"C:\Program Files (x86)\IIS Express\iisexpress" /path:\Publish /port:8080
This take access to sub-threads of the tree process of OS.
If you want to launch process and then make the process independent on the "launcher" / the originating call:
//Calling process
using (System.Diagnostics.Process ps = new System.Diagnostics.Process())
{
try
{
ps.StartInfo.WorkingDirectory = #"C:\Apps";
ps.StartInfo.FileName = #"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE"; //command
ps.StartInfo.Arguments = #"C:\Apps\xyz.accdb"; //argument
ps.StartInfo.UseShellExecute = false;
ps.StartInfo.RedirectStandardOutput = false;
ps.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
ps.StartInfo.CreateNoWindow = false; //display a windows
ps.Start();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("==> Process error <=={0}" + ex.ToString(), Environment.NewLine));
}
}