[STAThread] and remoting


This is one of the most common gotchas in remoting. VS by default adds a [STAThread] attribue to the Main method of an application. If this attribute is present on a remoting server implementation, and for some reason the main thread is not pumping messages, the finalizer thread will not run which will lead to huge memory leaks. [STAThread] should be avoided in almost all cases for console applications, and if its required the main thread should not block on something like Console.ReadLine.


Comments (4)
  1. According to the docs, this attribute only affects COM interop. I don’t see why, then, you’d ever apply it to anything but a class library that’s consumed by COM, so naturally, not applying it to console, windows, or (in general) web applications makes perfect sense.

  2. Strike that last comment. Still waking up I guess. 🙂

Comments are closed.

Skip to main content