You are making this harder than it needs to be.
Don't create a thread to start
ServiceHost
.ServiceHost
will manage its own threads internally for the service calls. There's no advantage to creating a new thread just to start theServiceHost
. Also your clientConnect()
call won't succeed until theServiceHost
is initialized and running. So there's required synchronization betweenserver.Start()
andclient.Connect()
. The new thread isn't saving you anything.Do not do Finalizers in C# unless you have a very good reason.
~Server()
is a bad idea.
So get rid of the Server
class entirely. The wrapper isn't buying you anything (unless you are doing configuration management that isn't shown in the posted code).
Create the service host in MyForm()
and call host.Close()
in MyForm_FormClosed()
. Done.