-2

I'm making an OPC client on winforms, client that connecting with server and reading data from it with OPC protocol. So, I successfull connecting to server, but when my program crashing or I'm closing it manually from task manager, connection with server is still exist (and that's not good).

Can I somehow check my program for correct ending ? And if my program ends incorrect then disconnect from server.

I tried to use myProcess.HasExited, but it's not working.

Phiter
  • 14,570
  • 14
  • 50
  • 84
Jam
  • 101
  • 11
  • Hi Ramon, please read this and edit your question accordingly: http://stackoverflow.com/help/how-to-ask – Manfred Radlwimmer May 04 '16 at 12:43
  • Since you've suggested a `Process` component to this question, I assume you are launching an EXE and asking how to monitor it. Why do that? Why is the EXE _itself_ not responsible for gracefully closing the connection when something goes wrong? It's highly unlikely you would be able to handle that from a distance... – DonBoitnott May 04 '16 at 15:32
  • I actually don't know. I thought so too – Jam May 05 '16 at 07:32

1 Answers1

-1

I can only guess you can use Socket for server connection. If so, then this is a good practice to close it once you've done with server call. You need to call sequence of Shutdown(), Close(), Dispose().

See this MSDN article: https://msdn.microsoft.com/en-us/library/wahsac9k(v=vs.110).aspx

zmechanic
  • 1,842
  • 21
  • 27
  • Thanks, but I'm using someone's homemade library for OPC, and there already had Disconnect methods. But i just dont know how to disconnect if program crash. – Jam May 04 '16 at 12:58
  • You need to subscribe to "catch all unhandled exception" events. This should help: http://stackoverflow.com/a/32396235/1786034 – zmechanic May 04 '16 at 13:02