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
}
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With