Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to receive arguments passed from a windows service 3

Status
Not open for further replies.

vatawna

Programmer
Feb 24, 2004
67
0
0
US
I have a windows service that listens to any data coming on port 8001. If data is received, it will activate an application and pass along a string that came on that port. I use Process.Start(file path, argument) to activate that application. But I don't know how to grap the argument in the activated application. I used the read method from the Console, Stream, or MemoryStream classes, but it doesn't work.

Does anyone know how to get arguments passed from a service? Or is there a better way to do this? My project is similar to an instant messenger.

Thanks.
 
What is the activated app? A windows or a console application ?
 
In the static void Main() method, there's a second version that accepts a string parameter, which is the commandline being passed to your app.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for responding. TipGiver, the activated app is a Windows app.

I ran into another problem. When I use Process.Start in the Windows service to activate the app, I can see the app name in Processes on Task Manager, but the app window never appears on the desktop or the taskbar. I also use StartInfo.WindowStyle = ProcessWindowStyle.Normal before Process.Start to make sure it show, but it doesn't. Do you know how to fix this?
 
The problem is : the windows service doesn't know what user to run the GUI as since the service itself is running as "System".

Launching a GUI from a windows service is generally a bad idea because the user may not even be logged in - then what?

So my recommendation to you is (and I just did this myself) write a "service application" that sits in the Startup folder and launches when the user logs in. This way you can also use things like a Notification icon in the system tray.
 
Oh and Chip is right.

Overload you Main() method in the app being launched as a process.

Main(string[] args)
{
//do something with your arguments.
}


Then when you launch the process, add

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.Arguments = "-t -n somepath.xml";

or whatever arguments you want.
 
JurkMonkey, that was a great idea. The reason I want to use Windows service is I have a loop to watch for any data on port 8001:

do{
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
}
while(myNetworkStream.DataAvailable);

This loop takes to much CPU resources. I tried it before without a Windows service, and it actually slows my computer performance down. If I use a notification app like you suggested, is there a way to not slow the computer down.
 
Use an asynchronous read instead of a blocking read. What happens in that case is you request the start of a read, then the OS puts your request on an internal queue. When the data actually arrives, it will call you back with what it received.

This is a much better way to do this, as it's low CPU usage compared to a blocking read or polling. The downside is that the coding is more complex. But still worthwhile to learn, as the same techniques can be used for other asynchronouse requests (network, disk i/o, http, etc).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top