Minimal Example

To run code in a separate process, we need to do 3 things:

  1. Define an interface to serve as the "communication protocol"

  2. Implement the interface in a class.

  3. Create an Instance using the interface and a class. Instance.Target will serve as a communication channel.

Here is a minimal example:

Copy C#
interface IPidReporter
{
    
int GetPid();
}

class PidReporter :
IPidReporter
{
    
public 
int GetPid()
    {
        
return
Process.GetCurrentProcess().Id;
    }
}

static 
class Program
{
    
static 
void Main()
    {
        
// Compare the PID of the a worker
process and the 
        
// executing process. They should
be different.
        

using(Instance<IPidReporter> instance =
           
Instance<IPidReporter>.Create<PidReporter>())
        {
            Console.WriteLine(Process.GetCurrentProcess().Id);
            Console.WriteLine(instance.Target.GetPid());
        }

        
// Creates a pool of 5 Instances.
The concept is 
        
// similiar to "Application Pools".
Pooled instances 
        
// will be automatically restarted
when a 
        
// TargetTerminatedException is
thrown.
        
// 
        
// (The Pool may reuse the same
Instance to 
        
// serve requests from the same
thread.)
        

using(Pool<IPidReporter> pool =
            Pool<IPidReporter>.Create<PidReporter>(
5))
        {
            Console.WriteLine(pool.Target.GetPid());
        }
    }
}

Specifying the location of worker process .exe file

By default, TaskGarden will create a file in the temporary directory to use as the .exe file for worker processes. It can be customized by passing a Template parameter to Instance:

Copy C#
using(Template tp = 
new Template(
"examplewp.exe"))

using(Instance<IPidReporter> instance =
    Instance<IPidReporter>.Create<PidReporter>(
new InstanceStartInfo(),
    tp))
{
    
// ...
}

Running code out-of-process as another user

Copy C#

using(Instance<IPidReporter> instance =
    Instance<IPidReporter>.Create<PidReporter>())
{
    
// Must be specified before the
first call to Target
    instance.StartInfo.UserName = 
"User";
    instance.StartInfo.Password = 
"password";
}

For programs running under Session 0 (such as ASP.NET Web applications), TaskGarden requires the TaskGardener helper service to be installed in the system in order to run a process as a different user. This is automatically installed by the installer.

For manual deployment scenarios without using the installer, there are 2 options:

  • Run TaskGardener.exe -install to install the service manually.
  • Run TaskGardener.exe from console as an administrative user. The program continue running in the background until terminated.

Manual deployment without using the installer

TaskGarden can be Xcopy deployed by copying TaskGarden.dll into the bin directory. For an ASP.NET deployment which will be run as another user in Session 0, you will also need to deploy TaskGardener.exe. Please refer to the section "Running code out-of-process as another user".

To insert your license, put the following in your application's initialization code:

Copy C#
// This method may throw
InvalidLicenseException
TaskGarden.Settings.InstallLicense(
"your-license-code-here");