How to wait for another process status in .NET?

I am working on a .NET integration project. The test environment executable starts the service, and then must wait for the operation to complete.

What is the best approach for exe to wait for the service to complete its task (the service itself will not exit after the task is completed)?

Both processes have access to the same database, so my first thought was a simple table that records the status of a service. As soon as he signals that this is done, exe can stop waiting and complete his task. Other approaches?

Let me repeat that the service, having completed its task, will remain operational / in memory, so waiting for it to exit will not work .; -)

In addition, it is purely for integration testing purposes and will never go into production, so β€œsimple” is the operational word.

+3
source share
4 answers

You can pass the name to the Semaphoreservice on the command line (or through some other mechanism, such as hard coding), and then wait for the service Release()by calling WaitOne()in your exe.

Application Code:

Semaphore s = new Semaphore(1, 1, "MyNamedSemaphore");
// start service, passing the string "MyNamedSemaphore"
s.WaitOne(); // will wait for Release() in service

Service Code:

// perform the initial task
// find semaphore name (i.e. from Environment.CommandLine)
Semaphore s = new Semaphore(1, 1, semaphoreName); // will use existing kernel object
s.Release(); // WaitOne in exe will complete
+5
source

WMI calls should give you what you need. You can catch started / completed events and do what you need from there. (Thanks to Chris Lively for showing me this)

http://weblogs.asp.net/whaggard/archive/2006/02/11/438006.aspx

System.Diagnostics.Processes , .

+1

?

, Event - , , , , . -, , . , , . ( testapp , , , . , , ).

: , auto- reset, "" , .

.NET, , Win32 CreateEvent, SetEvent WaitForSingleObject.

+1

Source: https://habr.com/ru/post/1697858/


All Articles