473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Starting a windows app from a service

Hello,
I hope this question isn't too fundamental. I have a feeling it is. I'm
trying to start a winform app from a windows service written in C#. I
created a class and used it from a windowsforms application and all was
well. But when I try to use the class in the OnStart method of a windows
service, the process I'm trying to start runs but the window never shows up.
Like I said, this may be fundamentally impossible/illogical to do but here's
the basic code I'm using:

public class AppServer
{

public void StartService()
{
string path;
string app;
ProcessStartInfo psi;

string[] settings =
System.Configuration.ConfigurationSettings.AppSett ings.GetValues("app");
for (int i = 0; i <= settings.GetUpperBound(0); i++)
{
path = settings[i].Substring(0, settings[i].LastIndexOf("\\"));
app = settings[i].Substring(settings[i].LastIndexOf("\\") + 1);
Directory.SetCurrentDirectory(path);
psi = new ProcessStartInfo(app);
psi.CreateNoWindow = false;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.WorkingDirectory = path;
Process.Start(psi);
}
}
}

Any comments much appreciated.

Thanks
Steve
Nov 16 '05 #1
5 4912
Firstly, check that the service is set to allow interaction with the
desktop. You can right click the service in the SCM and check it that
way.

Secondly, since the service is probably loading under the LocalSystem
account, it may not have the necessary priveleges to run the app. You
might try having the service start up as a different user.
Hope this gives you some ideas.

Nov 16 '05 #2
Steve,

You can do it, but I would recommend against it. You would have to go
to the properties for the service and check "allow service to interact with
the desktop". However, I strongly recommend against this. If you need to
communicate to a service, then have a separate application which uses a
technology like remoting to get information from it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve Long" <St**********@NoSpam.com> wrote in message
news:O$****************@tk2msftngp13.phx.gbl...
Hello,
I hope this question isn't too fundamental. I have a feeling it is. I'm
trying to start a winform app from a windows service written in C#. I
created a class and used it from a windowsforms application and all was
well. But when I try to use the class in the OnStart method of a windows
service, the process I'm trying to start runs but the window never shows
up.
Like I said, this may be fundamentally impossible/illogical to do but
here's
the basic code I'm using:

public class AppServer
{

public void StartService()
{
string path;
string app;
ProcessStartInfo psi;

string[] settings =
System.Configuration.ConfigurationSettings.AppSett ings.GetValues("app");
for (int i = 0; i <= settings.GetUpperBound(0); i++)
{
path = settings[i].Substring(0, settings[i].LastIndexOf("\\"));
app = settings[i].Substring(settings[i].LastIndexOf("\\") + 1);
Directory.SetCurrentDirectory(path);
psi = new ProcessStartInfo(app);
psi.CreateNoWindow = false;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.WorkingDirectory = path;
Process.Start(psi);
}
}
}

Any comments much appreciated.

Thanks
Steve

Nov 16 '05 #3

"Steve Long" <St**********@NoSpam.com> wrote in message
news:O$****************@tk2msftngp13.phx.gbl...
Hello,
I hope this question isn't too fundamental. I have a feeling it is. I'm
trying to start a winform app from a windows service written in C#. I
created a class and used it from a windowsforms application and all was
well. But when I try to use the class in the OnStart method of a windows
service, the process I'm trying to start runs but the window never shows
up.
Like I said, this may be fundamentally impossible/illogical to do but
here's
the basic code I'm using:

public class AppServer
{

public void StartService()
{
string path;
string app;
ProcessStartInfo psi;

string[] settings =
System.Configuration.ConfigurationSettings.AppSett ings.GetValues("app");
for (int i = 0; i <= settings.GetUpperBound(0); i++)
{
path = settings[i].Substring(0, settings[i].LastIndexOf("\\"));
app = settings[i].Substring(settings[i].LastIndexOf("\\") + 1);
Directory.SetCurrentDirectory(path);
psi = new ProcessStartInfo(app);
psi.CreateNoWindow = false;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.WorkingDirectory = path;
Process.Start(psi);
}
}
}

Any comments much appreciated.

Thanks
Steve

You shouldn't launch interactive processes from Windows Services, they are
designed to run even when there is no interactive session available.
The reason is simple, each service runs in an invisible secured desktop
environment (called a Winstation), programs launched from this share the
same desktop environment, so their UI is invisible to the user.
Note that you can flip the "Enable service to interact with desktop" service
attribute, but this should only be considered as a debugging aid .

Willy.

Nov 16 '05 #4
Very good feedback everybody. You might be right that it would be a bad idea
since the primary reason for me doing this is so that the service can launch
a desktop app without the server having to be logged into by someone. The
app in question, is by design, a windowed app and therefore can not really
run as a service. It does however, serve up some intensive data and it takes
a several seconds to load the app, which is why it must be running all the
time. Can't really add the load up time to the time it takes to perform the
spatial queries. Others communicate with this application via a .aspx page
that I set up which in turn communicates with the desktop app via .NET
remoting.
So, let's say I check the option to allow the service to interact with the
desktop, allowing this app to be started up without actually logging into
the server. What are the pitfalls with this course of action?

Steve

"Steve Long" <St**********@NoSpam.com> wrote in message
news:O$****************@tk2msftngp13.phx.gbl...
Hello,
I hope this question isn't too fundamental. I have a feeling it is. I'm
trying to start a winform app from a windows service written in C#. I
created a class and used it from a windowsforms application and all was
well. But when I try to use the class in the OnStart method of a windows
service, the process I'm trying to start runs but the window never shows up. Like I said, this may be fundamentally impossible/illogical to do but here's the basic code I'm using:

public class AppServer
{

public void StartService()
{
string path;
string app;
ProcessStartInfo psi;

string[] settings =
System.Configuration.ConfigurationSettings.AppSett ings.GetValues("app");
for (int i = 0; i <= settings.GetUpperBound(0); i++)
{
path = settings[i].Substring(0, settings[i].LastIndexOf("\\"));
app = settings[i].Substring(settings[i].LastIndexOf("\\") + 1);
Directory.SetCurrentDirectory(path);
psi = new ProcessStartInfo(app);
psi.CreateNoWindow = false;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.WorkingDirectory = path;
Process.Start(psi);
}
}
}

Any comments much appreciated.

Thanks
Steve

Nov 16 '05 #5

"Steve Long" <St**********@NoSpam.com> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
Very good feedback everybody. You might be right that it would be a bad
idea
since the primary reason for me doing this is so that the service can
launch
a desktop app without the server having to be logged into by someone. The
app in question, is by design, a windowed app and therefore can not really
run as a service. It does however, serve up some intensive data and it
takes
a several seconds to load the app, which is why it must be running all the
time. Can't really add the load up time to the time it takes to perform
the
spatial queries. Others communicate with this application via a .aspx page
that I set up which in turn communicates with the desktop app via .NET
remoting.
So, let's say I check the option to allow the service to interact with the
desktop, allowing this app to be started up without actually logging into
the server. What are the pitfalls with this course of action?

Steve

If it's designed to be a windowed application, it expects some form of human
interaction.
That means it is free to show dialog (modal as well a modeless) boxes (in
normal cases or in case of failures), dialog boxes that don't get closed as
there is no-one available to click a button or press a key.
It also expects the user profile to be available and accessible, which is
not the case as it runs in the context of a non interactive user
(LocalSystem).
It also expect to have access to the HKCU registry hive, which again is not
the case as the hive is not loaded.
These are some of the potential problems, but there could be more, for
instance what resources (local and remote) is the application (running as
LocalSystem) required to have access to.

Willy.
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Glenn | last post by:
I have used this code successfully in a form application. I tried to add the same code in a service and have not been able to get the application to start. I have the service starting with a local...
2
by: karl | last post by:
I have a windows service that kicks off a 'monitor' thread which in turn kicks off 4 additional threads. These 4 threads basically are listen on a designated socket and report back any errors...
2
by: Razzie | last post by:
Hey all, I wrote a Windows Service. When I test it on my developement machine (winXP) it works fine. It starts ok, never crashes, etc. When I install the service on another machine (win2000) it...
4
by: Kristof Despiere | last post by:
Suppose you have one domain, filled with a couple of users. What needs to be done now is I need to start a windows application from a webform by pressing a button on the webform (for example). ...
0
by: tshad | last post by:
I have a Windows Service I created that just sets a timer and writes to EventLog every 10 seconds. It installed fine and it actually starts. But it says it doesn't. The progress bar shows the...
4
by: hogcods | last post by:
I'm having a strange problem with a service I developed. After installing the service on the target machine, I'm trying to start it using the Services admin window. I get the "Windows is starting"...
3
by: Johnny Fugazzi | last post by:
OK, I created my handy little windows service and created an MSI installer using the Deployment tool in VS.net with Custom Actions according to the msdn help. Worked great, with one little...
6
by: Arnie | last post by:
We're using the ServiceController class provided by the .NET Framework, programming in C#. We are using the Start() method to start a service from another service. This works fine most of the...
5
by: Benzi Eilon | last post by:
I have written a C# application which should run as a Windows Service. I must avoid having multiple instances of the application on one machine so I inserted the following code at the beginning of...
5
by: eliasen | last post by:
Hi I have created a Windows Service using C# and .NET2.0. The service is quite simple - right now it doesn't do anything except throwing an exception in the OnStart method. It used to something...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.