Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a new Process that executes a delegate

Is is possible in .NET to execute a method (delegate, static method, whatever) in a child process? System.Diagnostics.Process seems to require an actual filename, meaning that a separate executable is needed.

What I am trying to do is to verify, in a unit test, that an OS resource is cleaned up on process exit. I know that could use CodeDOM or IL generation to create such an assembly and execute it, but the whole point of unit tests is to isolate component parts, not to create complexity. For the same reason, I'd like to avoid a separate assembly altogether.

Ideally, I would do something like:

public static void CreateCounter()
{
    var counter = new PerformanceCounter("category", "counter", "instance");
    counter.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
}

[Test]
public void TestResourceDisposal()
{
    // Start child process to execute CreateCounter()
    ...
    // verify the resource is disposed
}
like image 347
Ben Avatar asked Mar 22 '11 00:03

Ben


People also ask

How do you call a delegate function in C#?

Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.

What is a delegate function?

Delegates Overview Delegates are similar to C++ function pointers, but delegates are fully object-oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method. Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods.

What is delegate in C# with example?

Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate. The following example declares a delegate named Del that can encapsulate a method that takes a string as an argument and returns void: C# Copy.


1 Answers

First of all, no, there's no way to do this. I process implies a .exe. This isn't Unix where you can fork a process that's a copy of the parent.

I'd just create a tiny .exe to run. Do you need to run the test with different performance counters? If it works with one, then surely it will work with any of them?

like image 60
John Saunders Avatar answered Sep 19 '22 14:09

John Saunders