C# WinForms: How to set Main function STAThreadAttribute

Solution very easy; Just add this on top of the Main method [STAThread]

So your main method should look like this

 static void Main(string[] args)

It works for me.

ShowDialog() shouldn't be called from a background thread - use Invoke(..).

Invoke((Action)(() => { saveFileDialog.ShowDialog() }));

this should work if you are creating the thread in which you call the showDialog:

var thread = new Thread(new ParameterizedThreadStart(param => { saveFileDialog.ShowDialog(); }));

Add following code on FormLoad

private void Form1_Load(object sender, EventArgs e)
    Thread myth;
    myth = new Thread(new System.Threading.ThreadStart(CallSaveDialog)); 
    myth.ApartmentState = ApartmentState.STA;

Here CallSaveDialog is a thread and here you can call ShowDialog like this

void CallSaveDialog(){saveFileDialog.ShowDialog();}

On your MainForm:

if (this.InvokeRequired) { 
} else { 

Or, if you will have other methods that need to be run from the UI thread:

  private void DoOnUIThread(MethodInvoker d) {
     if (this.InvokeRequired) { this.Invoke(d); } else { d(); }

Then, call your method as such:

 DoOnUIThread(delegate() {


  1. Joey

    • 2018/9/23

    C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone 

  2. Jett

    • 2017/7/26

    News Citigroup Inc.C. Consumer-Service Stocks Have Underperformed. Why It's Their Time to Shine. The Price to Earnings (P/E) ratio, a key valuation measure, is calculated by dividing the stock's

  3. Hassan

    • 2018/12/31

    C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

  4. Harlem

    • 2018/10/13

    Citigroup, Inc. Common Stock (C) Stock Quotes - Nasdaq offers stock quotes & market activity data for US and global markets.

  5. Colton

    • 2020/1/25

    learn-c.org is a free interactive C tutorial for people who want to learn C, fast.

  6. Smith

    • 2019/10/11

    Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.

  7. Dallas

    • 2020/11/13

    Amazon.com: C Programming Language, 2nd Edition: 8601410794231: Brian W. Kernighan, Dennis M. Ritchie: Books.

  8. Malcolm

    • 2017/6/19

    The Centers for Disease Control and Prevention (CDC) cannot attest to the accuracy of a non-federal website. Linking to a non-federal website does not constitute an endorsement by CDC or any of its employees of the sponsors or the information and products presented on the website. You will be

  9. Edwards

    • 2016/9/15

    C is considered as a middle-level language because it supports the feature of both low-level and high-level languages. C language program is converted into 

  10. Bryson

    • 2018/5/27

    c = a + b; Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. C/C++ has many built-in operator types and they are classified as follows:

  11. Otis

    • 2019/5/30

    The best site for C and C++ programming. Popular, beginner-friendly C and C++ tutorials to help you become an expert!

  12. Rory

    • 2021/5/25

    C. diff (also known as Clostridioides difficile or C. difficile) is a germ (bacterium) that causes severe diarrhea and colitis (an inflammation of the colon). It’s estimated to cause almost half a million infections in the United States each year. About 1 in 6 patients who get C. diff will get it again in the subsequent 2-8 weeks.

Comments are closed.

Recent Posts