Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are WinForms applications STAThread by default?

Tags:

When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class.

I have been reading some docs about it, but I'm not sure if I understood it at all.

Really I have some questions about it:

  1. Why is this attribute added?
  2. What does it mean?
  3. What happens if you remove this attribute?
like image 389
Daniel Peñalba Avatar asked Jan 11 '11 15:01

Daniel Peñalba


People also ask

Are WinForms deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.

How are Windows Forms applications developed?

You ordinarily build Windows Forms apps by adding controls to forms and developing responses to user actions, such as mouse clicks or key presses. A control is a discrete UI element that displays data or accepts data input. When a user does something to your form or one of its controls, the action generates an event.

Does Microsoft still support WinForms?

"We continue to support and innovate in Windows Forms runtime," said Microsoft's Igor Velikorossov last month in announcing what's new for WinForms in . NET 6. He's a software engineer on the dev team for the 19-year-old product, a free and open-source graphical (GUI) class library included as a part of .

What is the replacement for WinForms?

WPF, would be your answer to Windows Forms if you are on the . NET platform.


1 Answers

To quote from an MSDN blog,

When the STAThreadAttribute is applied, it changes the apartment state of the current thread to be single threaded. Without getting into a huge discussion about COM and threading, this attribute ensures the communication mechanism between the current thread and other threads that may want to talk to it via COM. When you're using Windows Forms, depending on the feature you're using, it may be using COM interop in order to communicate with operating system components. Good examples of this are the Clipboard and the File Dialogs.

like image 144
Alex Fort Avatar answered Sep 22 '22 15:09

Alex Fort