using System;
using System.IO;
namespace HeuristicLab.PluginInfrastructure {
[Serializable]
public class ApplicationRunner : Runner {
///
/// Arguments for the StartApplication.
///
public ICommandLineArgument[] Args { get; set; }
///
/// The application which should run in child process.
///
public IApplication StartApplication {
get {
if (application == null) {
using (var memStream = new MemoryStream(serializedStartApplication)) {
application = (IApplication)formatter.Deserialize(memStream);
}
}
return application;
}
set {
using (var ms = new MemoryStream()) {
formatter.Serialize(ms, value);
serializedStartApplication = ms.ToArray();
}
}
}
// Encapsulated application is necessary, because it is not possible to
// instantly deserialize the application, before all assemblies are loaded.
private byte[] serializedStartApplication = new byte[0];
// cache application to prevent new instances every get call of StartApplication
private IApplication application;
protected override void Execute() {
StartApplication.Run(Args);
}
protected override void OnRunnerJob(RunnerJob runnerJob) {
switch (runnerJob) {
case RunnerJob.Cancel: StartApplication.OnCancel(); break;
case RunnerJob.Pause: StartApplication.OnPause(); break;
case RunnerJob.Resume: StartApplication.OnResume(); break;
}
}
}
}