Windows Service access to variables/properties By Client Applicati

G

Guest

How do I access Public variables within a Windows Service?

I can, and have, created installed and executed a basic Service. From my
client App I can Start, Stop...etc the service.... What I want to do is to
read properties that are in the service. for instance if the service had a
Propertie or Public variable like

Public Int Myint = 12; (or... Public Myint as integer = 12)


Then how could I read that variable from a typical windows Form application?

The basic Idea is to have a service churning away all the time and then when
a client app OR mulitple client apps need info that the service is
calculating then the client app should be able to simple reference the
variable and read it .... no?
 
M

Mehdi

How do I access Public variables within a Windows Service?

Public Int Myint = 12; (or... Public Myint as integer = 12)
Then how could I read that variable from a typical windows Form application?

You can't. A windows service is almost just a normal application except
that it runs in the background and has no UI. You can't access variables
from another application just like that. That would be too easy :)
The basic Idea is to have a service churning away all the time and then when
a client app OR mulitple client apps need info that the service is
calculating then the client app should be able to simple reference the
variable and read it .... no?

No. An easy enough solution (among others) would be to expose an object
from your Windows Service through .NET Remoting. Your client applications
would then just need to connect to your .NET Remoting server and call
methods of the exposed object. These methods can return whatever values
they want. You effectively end up with a Client/Server system. Have a look
on the Web for .NET Remoting, there are loads of articles and tutorials to
do just what you want. Or get the excellent Ingo Rammer's book: Advanced
..NET Remoting.

Other than that, you could build a similar system but using raw sockets
instead of .NET Remoting (although i don't really see why you would want to
do that), use shared memory (that will require you to use the Win32 API
through interop since there are no .NET wrappers for this yet), have your
service write a file and your client applications read this file...
 
R

Robbe Morris [C# MVP]

That would be crossing the app domain at run-time.
I believe you can do this but it isn't recommended and
can be a bit complex to implement reliably.

I think what a lot of people do is have the service
listen on a TCP socket. Your client apps would
open the socket, send a message, the service responds
back on the socket.

This should prove useful:

http://www.eggheadcafe.com/articles/20020323.asp

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top