Link to home
Start Free TrialLog in
Avatar of jindalee
jindalee

asked on

Running an exe file as a servcie

I have an application I am building that a customer wants to run both as a service (without windows, dialogs etc) and, occasionally, as a full blown Windows application with all the bells and whistles.

Their idea is that they can schedule a job to run when the network is quiet but also have the ability, if required, for an admin user to run the program.

Is this possible or do I have to re-write as a service?
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there.
As an alternative, you could load the EXE from the Windows service using AppDomain class. check out the example code I gave. This way, you can run the EXE from the windows service, plus you could run the EXE manually. Alos check out these links:
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx
http://it.toolbox.com/blogs/programming-life/enter-the-net-appdomain-5595
Cheers.
Jas.
 

Module Module1
 
    Sub Main()
        Dim newDomain As AppDomain = AppDomain.CreateDomain("MyNewDomain")
 
        newDomain.Load("C:\TheExeToRun.exe")
 
    End Sub
 
End Module

Open in new window

Avatar of oobayly
You could create the app as a Console App, which inherits System.ServiceProcess.ServiceBase.
Then, depending on which arguments are passed to the Main(args() as string) method, you can either start it as a service, or as a windows form application
class Foo inherits System.ServiceProcess.ServiceBase
  public shared sub Main(args() as string)
    if (args.length == 0) then
      System.ServiceProcess.ServiceBase.Run(new Foo())
    else
      Application.Run(new Form1())
    end if
  end sub
 
  protected override sub OnStart(args() as string)
  end sub
 
  protected override sub OnStop()
  end sub
end class

Open in new window

Oops, my Vb is a tad rusty
class Foo inherits System.ServiceProcess.ServiceBase
  public shared sub Main(args as string())
    if (args.length = 0) then
      System.ServiceProcess.ServiceBase.Run(new Foo())
    else
      Application.Run(new Form1())
    end if
  end sub
 
  protected override sub OnStart(args as string())
  end sub
 
  protected override sub OnStop()
  end sub
end class

Open in new window

Avatar of jindalee
jindalee

ASKER

I'll have to translate from C# ti VB I guess butthis looks to me to be the best answer