Jump to content

Windows Service Events

cluelessgenius

so i never touched windows services before but now i thought id give it a try.

 

can someone tell me how to get data from the service to a desktop application? the easiest/quickest way?

 

i found a bunch of stuff online but i feel like theres too many option or too much information.

 

in c# i can just subscribe to an event from another class. does it work the same for services.

 

im trying to have my service listen to a websocket and then raise an event when there a new message. 

then id want that message forwarded to my desktop application maybe wpf or uwp or maybe winforms im not sure yet.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just bind a socket to a port and then listen for traffic. You will need to allow incoming connections in the firewall. 

 

You may also need to configure the router if your machine is behind a NAT. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, wasab said:

Just bind a socket to a port and then listen for traffic. You will need to allow incoming connections in the firewall. 

 

You may also need to configure the router if your machine is behind a NAT. 

i think i found a simpler way. cant i just write into a evet log and read that?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

49 minutes ago, cluelessgenius said:

i think i found a simpler way. cant i just write into a evet log and read that?

If you are talking about what messages are recieve by another proccess over the networks then yeah, just parse the log file outputed by that proccess if it has any. Otherwise use some network analysis tools like Wireshark to sniff the packets and then manually dump it to a log. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, cluelessgenius said:

i think i found a simpler way. cant i just write into a evet log and read that?

No you wouldn't want to do that. Service are technically for read only.

What you should do is use a communication protocol of some sort.

Windows Service are well suited applications to use MSMQ.

 

You can also use any of the Interprocess communication like NPT (which is similar to how MSMQ works)

 

MSMQ is probably the easiest option. The service keeps writing to the MSMQ Queue and that it.

 

The Client app just keep reading from it for new messages.

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, wasab said:

If you are talking about what messages are recieve by another proccess over the networks then yeah, just parse the log file outputed by that proccess if it has any. Otherwise use some network analysis tools like Wireshark to sniff the packets and then manually dump it to a log. 

im not even talking network at all. everthing on a single system

but i seem to have problems writing to an event log and i have no clue how to debug a service on runtime

 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, cluelessgenius said:

im not even talking network at all. 

Yes you were. 

4 hours ago, cluelessgenius said:

my service listen to a websocket

https://en.m.wikipedia.org/wiki/Network_socket

 

Your proccesses can in fact talk with each other on the same computer using network protocol. 

 

You can also use signals, pipes, shared memory, or whatever the windows equivalent of these are if you need two processes communicating with each other. 

 

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, cluelessgenius said:

i seem to have problems writing to an event log and i have no clue how to debug a service on runtime

 

Just use a debugger which is often ship in an IDE. A service is just a process of a program, no different than any other program.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, cluelessgenius said:

i have no clue how to debug a service on runtime

What language are you using for your service. The method varies.

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, wasab said:

Yes you were. 

https://en.m.wikipedia.org/wiki/Network_socket

 

Your proccesses can in fact talk with each other on the same computer using network protocol. 

 

You can also use signals, pipes, shared memory, or whatever the windows equivalent of these are if you need two processes communicating with each other. 

 

 

alright i get how that might have been misleading my service does use websockets and listen to online channels but the data transfer im talking about is just the other end of it once i have the online message i need to transfer it from the service to a desktop application on the same system. also im aware you can use network protocol within the same system but i thought maybe theres a easier more straight  forward way.

15 hours ago, Franck said:

What language are you using for your service. The method varies.

c#, c# everything since thats where most of my expertise lies

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, cluelessgenius said:

also im aware you can use network protocol within the same system but i thought maybe theres a easier more straight  forward way.

A service cannot send data directly to another process. You need to use a method. There is many here's a few

- Write a file to the disk, other apps read from it in read only

- Stream files. Same principle as above but more complex

- IPC = InterProcess Communication which is meant for InterProcess communication.

- WCF service self host with duplex connection (p2p)

- Write to a database and have client pull data every x seconds to check for new stuff

 

NPT is a nice IPC. The way to see it is that it creates a pipe that has a specific name and other application can connect to that pipe to be an end point. When the head sends something, everyone connected as a client to it receives it.

 

Better option is MSMQ which stand for Microsoft Message Queue. It creates a message queue with a unique name on the computer. Then applications (this include your service) can read what's in that message queue or add to that queue. Since it's a queue it's a FIFO collection (First in first out). MSMQ is ALSO accessible if you want from another computer on the same network so it has upgradability path.

 

NPT is great but a bitch if you want to make it work over network later on. The multithreading required to make it work is a lot of work especially with Stream what are incomplete or not. But it is simple to understand.

 

MSMQ basic implementation to make for example a chat program out of it is probably around 60 lines of code including brackets, namespace and usings

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Franck said:

A service cannot send data directly to another process. You need to use a method. There is many here's a few

- Write a file to the disk, other apps read from it in read only

- Stream files. Same principle as above but more complex

- IPC = InterProcess Communication which is meant for InterProcess communication.

- WCF service self host with duplex connection (p2p)

- Write to a database and have client pull data every x seconds to check for new stuff

 

NPT is a nice IPC. The way to see it is that it creates a pipe that has a specific name and other application can connect to that pipe to be an end point. When the head sends something, everyone connected as a client to it receives it.

 

Better option is MSMQ which stand for Microsoft Message Queue. It creates a message queue with a unique name on the computer. Then applications (this include your service) can read what's in that message queue or add to that queue. Since it's a queue it's a FIFO collection (First in first out). MSMQ is ALSO accessible if you want from another computer on the same network so it has upgradability path.

 

NPT is great but a bitch if you want to make it work over network later on. The multithreading required to make it work is a lot of work especially with Stream what are incomplete or not. But it is simple to understand.

 

MSMQ basic implementation to make for example a chat program out of it is probably around 60 lines of code including brackets, namespace and usings

so npt, msmq or wcf? which is easier on the system msmq looked like i need to install some sort of server? im a bit limited with hardware here or at least im trying to conserve performance.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, cluelessgenius said:

so npt, msmq or wcf? which is easier on the system msmq looked like i need to install some sort of server? im a bit limited with hardware here or at least im trying to conserve performance.

No you don't need any server. It's an addon on windows feature

image.png.574a14aaf2aab83cf7558c87ff53be79.png

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×