-2

I have two windows C# WinForms applications that behave the same way related to this issue. The behavior exhibits if the applications are ran in this order: Main application starts up and part of its normal working process opens some sockets; mixture of tcp udp and multicast. At some time pressing a button it starts another application using C# process related libraries. The second app starts ok and all is ok.

It is known and acceptable that if I start a second instance of my main app the second instance won't be able to use the sockets the first one owns. The issue that I have is that if I shutdown the first app and restart, it won't be able to acquire the sockets anymore... until the child app is also shutdown. Note that the second app doesn't use ANY sockets whatsoever, but somehow windows keeps the sockets locked until the second app shuts down.

I HAVE THIS QUESTION RELATED TO PROGRAMMING IN C# WIN FORMS WHICH IS ON THE TOPIC NOW WITH CAPITAL LETTERS FOR PEOPLE WHO CAN NOT DISTINGUISH ON/OFF TOPIC:

how do I solve this so that I don't need to shutdown the second app for the first one to be able to acquire its sockets (WHICH -i know - ARE FREE).

Gogu CelMare
  • 699
  • 6
  • 15
  • 1
    Crystal ball says that you set ProcessStartInfo.UseShellExecute to *false*. Don't do that. – Hans Passant Aug 08 '16 at 12:56
  • Cristal ball? What programming language is that? Is that (MORE) ON THE TOPIC of helping others find answers to their questions? . This question and its answer may be also helpful for people using .NET but not having too much experience with win32. It did help me. – Gogu CelMare Aug 11 '16 at 23:50

1 Answers1

1

This is normal handle inheritance behavior. Unfortunately the .Net Process.Start is passing true as bInheritHandles to CreateProcess (NB. an open request to allow control of this behavior exists: Make Process.Start have a option to change handle inheritance). As a work around, use the native CreateProcess instead, see When System.Diagnostics.Process creates a process, it inherits inheritable handles from the parent process.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Thank you Remus. This solves my problem of trying to track the external apps that I create. – Gogu CelMare Aug 09 '16 at 01:17
  • @GoguCelMare I think you should also read http://stackoverflow.com/a/4657392/105929 it may be what you need. – Remus Rusanu Aug 09 '16 at 05:28
  • Thank you. Remus. My problem was only with sockets ownership. So the solution of creating processes using Kernel32 CreateProcess with a false inheritsHandles has fixed my issues. – Gogu CelMare Aug 11 '16 at 23:35