The control program would be a console application. I would move most of
the code which currently resides in my service into the control program.
The service would run the control program.
The following code is my existing service. In my proposed solution the
AltaManager class and the remoting setup code in OnStart would move into the
control program. For this to work the control program and maybe the service
would have to use the same user account that is normally used to run my
application.
namespace AltaService
{
public class AltaRemotingService : System.ServiceProcess.ServiceBase
{
private static EventLog evt = new EventLog( "Application" );
public static String SVC_NAME = "Alta Remote Manager";
public AltaRemotingService()
{
this.ServiceName = SVC_NAME;
}
static void Main()
{
evt.Source = SVC_NAME;
evt.WriteEntry( "Alta Remoting Service initializing" );
ServiceBase.Run( new AltaRemotingService());
}
protected override void OnStart(string[] args)
{
try
{
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile );
}
catch( Exception e )
{
evt.WriteEntry( e.Message, EventLogEntryType.Error );
}
evt.WriteEntry( "Alta Remoting Service started" );
}
protected override void OnStop()
{
evt.WriteEntry( "Alta Remoting Service stopped" );
}
}
public class AltaManager : MarshalByRefObject
{
private static EventLog evt = new EventLog( "Application" );
private Settings _settings;
public AltaManager()
{
evt.Source = AltaRemotingService.SVC_NAME;
}
public void Startup()
{
LoadSettings();
Process myProcess = new Process();
myProcess.StartInfo.FileName = _settings.FileName;
try
{
myProcess.Start();
}
catch( Exception e )
{
evt.WriteEntry( e.Message + " " + _settings.FileName,
EventLogEntryType.Error );
}
}
public void Shutdown()
{
LoadSettings();
Process[] p;
p = Process.GetProcessesByName( _settings.AppName );
if( p.Length == 0 )
{
evt.WriteEntry( _settings.AppName + " process not found",
EventLogEntryType.Error );
}
else
{
try
{
for( int j = 0 ; j < p.Length ; j++ )
{
if( p[j].Responding )
{
p[j].CloseMainWindow();
}
else
{
p[j].Kill();
}
}
}
catch( Exception e )
{
evt.WriteEntry( e.Message + " " + _settings.AppName,
EventLogEntryType.Error );
}
}
}
public bool Running
{
get
{
LoadSettings();
Process[] p;
p = Process.GetProcessesByName( _settings.AppName );
if( p.Length == 0 )
{
return( false );
}
else
{
return( true );
}
}
}
private void LoadSettings()
{
try
{
XmlSerializer serializer = new XmlSerializer( typeof(Settings) );
TextReader reader = new StreamReader( "AltaSettings.xml" );
_settings = (Settings)serializer.Deserialize( reader );
reader.Close();
}
catch
{
_settings = new Settings();
SaveSettings();
}
}
private void SaveSettings()
{
XmlSerializer serializer = new XmlSerializer( typeof(Settings) );
TextWriter writer = new StreamWriter( "AltaSettings.xml" );
serializer.Serialize( writer, _settings );
writer.Close();
}
}
}
I haven't tried creating the control program, since I am busy trying to
figure out how to create an install program. I was thinking that I was
going to have to write some code to call installutil and put that in as a
custom action. Yesterday, down in the depths of the Creating Installation
Components documentation, I discovered that all I have to do is add the exe
which contains the service and Installer as a custom action. Its great that
Visual Studio makes this so simple, too bad it wasn't easy to find the
information in the documentation.
Bill
"Jeffrey Tan[MSFT]" said:
Hi Bill,
I am glad, you got some soluitons.
For your second soluiton, as you said, you can not invoke GUI application
through Process class in your service, how do you invoke your control
program in your service?
Is it a console application, so can be invoke?
What does your control program "should be less than a days work." mean?
I am just curious about your soluiton
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.