Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra steps when shutting down thread that used WCF?

Is there anything special I should be doing to gracefully shut down a thread when it has performed a WCF call during its processing?

I seem to be getting a memory leak on my server and I have tracked it down to making a WCF call from my worker threads. I create the threads in a simple way, like this...

var schedule = new Schedule();
var scheduleThread = new Thread(New ParameterizedThreadStart(schedule.Run));
scheduleThread.SetApartmentState(ApartmentState.STA);
scheduleThread.Priority = ThreadPriority.Lowest;
scheduleThread.Start(null);

...and the code for executing my test code that has the issue...

public void Run(object param)
{
    var wcf = new TestServer.TestServerClient(...);
    wcf.Open();
    wcf.Ping();
    wcf.Close();
}

...after running it 2000 times I can see using the memory profiler that there are 2000 instances of the following classes...

DispatcherOperationCallback
IntPtr
HwndSubclass
NativeMethods.WndProc

So, is there some cleanup I should be performing relating to using WCF from a thread? Calling GC.Collect() has no impact.

like image 834
Phil Wright Avatar asked Nov 04 '22 04:11

Phil Wright


1 Answers

Have you considered using a "using" keyword on every object whose type implements IDisposable?

I hear it's better to use MTA server-side.

I also don't see where you perform thread synchronization.

like image 106
GregC Avatar answered Nov 13 '22 21:11

GregC