Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running an exe file as a servcie

Posted on 2009-04-13
5
Medium Priority
?
194 Views
Last Modified: 2013-11-27
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?
0
Comment
Question by:jindalee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 2000 total points
ID: 24128613
you can call your exe file from windows service but its not a good idea, rather, you re-write windows services. If you wants to look how to write windows service, have a look at

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24280009.html


0
 
LVL 10

Expert Comment

by:Jason Evans
ID: 24129066
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

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24137526
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

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24137549
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

0
 
LVL 1

Author Closing Comment

by:jindalee
ID: 31569422
I'll have to translate from C# ti VB I guess butthis looks to me to be the best answer
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question