running processes step by step - c#

I need to install mysql in my system in the first step
After installing or modifying the mysql in the system , I need to the run the data base scripts.
The problem here is db scripts started running before mysql setup completes.
I have tried different ways.. using auto run methods...using processes etc
can anybody tell me how to capture the (child)process id which is invoked by the current process(parent). Here in my case parent is invoking child process and exiting.

System.Diagnostics.Process has a method WaitForExit.
Edit: To also watch for subprocesses, you can use Win32 Job objects. If you create a process in a job, all of its child processes will also belong to the job (unless someone explicitly detaches them). You can monitor the job for events such as process creation and termination. There is a C# wrapper for job objects, JobObjectWrapper.

Related

Best way to delegate work to child process?

I need to do a lot of work in UI thread. This work consumes a lot of CPU and memory resources. In current realization I use Dispatcher.BeginInvoke() method, but it makes my application frozen.
What if I will delegate this job to child process without GUI? Main process run child process. After that main application sends some commands to child process, child process do some job and returns result object to host process?
How to communicate between host and child processes? I know about .NET remoting (marshaling), but it obsolete method (or not?).
Thanks and sorry for my bad english.
I've made a child process management library where the parent process and the child process are monitored due a bidirectional WCF pipe. If either the child process terminates or the parent process terminates each other is notified.
There is also a debugger helper available which automatically attaches the VS debugger to the started child process
You have a extendable communication infrastructure between the processes. No need for WCF knowledge.
Project site:
http://www.crawler-lib.net/child-processes
NuGet Packages:
https://www.nuget.org/packages/ChildProcesses
https://www.nuget.org/packages/ChildProcesses.VisualStudioDebug/

how can i find the instanceid/correlation ID of the scheduled Task that started my process

if i have a windows scheduled task that runs my EXE. is there a way from inside my EXE to find the scheduled task instance that triggered me?
Easy answer for that would be: no.
Best thing you could do is programatically access either the task scheduler library and see whether the process you are is in "running" mode, get the PID of the process (scheduler actually writes it out in its history) and compare it to yours.
Easier thing to do would be accessing to system event logs and seeing if there's any mention of execution your exe file (though they may not be such a log if the exe was actually executed).
The only thing you can associate with your process is the caller (which might be NETWORK SERVICE or some predefined account) which do not give any information regarding it being scheduled.
I tried for a while to solve this same issue, however I don't think it's possible using the current API. If you have a reference to the running task, you can get the PID of the task engine that started it. From there, you can to go to the task event log and look up the latest events with ID 200 (Action Started) having the same engine PID, however since you can have multiple task processes running beneath a single task engine, you can't go any farther with absolute certainty (e.g., a second instance of your process could be running under the same engine PID -- you won't be able to tell which correlation ID belongs to your target process.)

Process.Kill() and process events

Here is the situation:
I have to create a program that would input another processes output stream into textbox. That it self wouldn't cause too much problem. What does, however, is the fact that I have to run 5 instances of this console application and redirect output to 5 textboxes, as well as to be able to kill any of these processes at any time. As far as I have learned, the best way to do this is asynchronously. But the problem here is with killing processes, that are created on different thread. How do I kill it without having access to it since it doesn't exist in scope where I have to kill it. My best guess is to get its PID on Process.Start(), so I can kill it, so...
Is it possible to fire any event from process on Process.kill() command?
And if not - is there a way to kill a process in about the same time interval as Process.Kill() that does fire some sort of event?
Or maybe someone could suggest me some other approaches or best practice on how these problems are usually solved?
EDIT: The reason I am running all processes on different threads is that I use Thread.Sleep() on some of them if there is and input parameter that tell me that the process must be killed after x seconds.
Process.Kill() command for some reason, does, in fact, fire process exited event. Easiest way for me to know that the process was killed, was by making a volatile string that holds information about how it ended. (Change it to "killed" before process.kill etc...)
First of all you do not need Threads at all. Starting a process is async in itself, so Process.Start(...) does not block and you can create as many processes as you want.
Instead of using the static Process.Start method you should consider creating Process class instances and set the CanRaiseEvents property to true. Further there are a couple of events you can register (per instance) - those will only raise if CanRaiseEvents is set to true, but also after a process is/has exited (including Kill() calls).
When you call
Process.Start()
it returns a Process class instance, which you can use to retrieve information from it output and kill that process any time
Process p = Process.Start("process.exe");
//some operations with process, may be in another thread
p.Kill()

Process Analysis Services Database With C# Is Freezing

I'm using the Microsoft.AnalysisServices.Server class to connect to an instance of SSAS.
I retrieve the database by name and call the Process method with ProcessType.ProcessFull as the option.
server.Databases.FindByName("MyDatabase").Process(ProcessType.ProcessFull)
The program hangs for an hour, and afterwards the database will not be processed.
I also tried passing an XMLA command to process the database using the Server class. This command works when run in Sql Server Management Studio, but does not run when I pass it in through the server connection.
server.Execute("My XMLA Process Command")
I use server.Execute to pass the XMLA command to create the database before I try to process it. The create database command works fine, but the process database command will not work using either XMLA or the C# objects. It just freezes the program for an hour and the database is not processed.
Any help would be greatly appreciated. Thanks in advance.
Full processing can take quite awhile depending on the size of your data.
If you're creating a gui app and it's not responding, it could be because you're doing an extremely long running task on the main thread. Try something like a BackgroundWorker or ThreadPool.QueueUserWorkitem to run it in the background.
To see what's going on, you can get progress by subscribing to the progress events and updating your UI in the main thread.
Here's a thread that describes subscribing to those progress trace events:
http://social.msdn.microsoft.com/forums/en-US/sqlanalysisservices/thread/e9f87b28-a8c2-40fc-b6e6-68dfa59b07a6

Process management in .NET

So i have a helper process written in C++ and I open it, feed it arguments, and it feeds my program back information through the standardoutput stream.
PS. I don't have the source for the helper process exe.
If my application were to be terminated from the task manager, or for some reason crash, how could I ensure that my helper exe is closed? Is this possible? Would I need an external file?
Use Job Objects to manage groups of processes. In this case you want to create a job object using CreateJobObject, use SetInformationJobObject to set the JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag, and assign the helper process to the job using AssignProcessToJobObject. Don't close the handle to the job object unless you want to kill the helper process. When your process terminates (through any means), the handle will be closed and your helper process will be killed.
You should create an inheritable duplicate of the parent process handle and pass its value to the helper process on the commandline. The helper process can then wait on that handle on a separate thread (or the main thread if you're clever). When the handle becomes signaled, it means that the launching process has terminated.
Edit
Since you can't change the helper process, your options are more limited. Your could try attaching a handler to the launching process's OnAppDomainUnloaded event, but I'm not sure this will work in all the cases you're concerned about. You could also create a third process to monitor the first. This process would work as I described above. If you wanted to get really fancy, you could inject a remote thread into the helper process to monitor the parent. This is very technical, so I recommend against it.
The easiest way would be to close it on normal application exit and when AppDomain.CurrentDomain.UnhandledException is invoked (i.e. your app is about to crash)

Categories