1

I have an app which crashes when trying to open a SaveFileDialog. I searched for answers and I found that I needed to put this in a new thread, and that's what I did but I had an error about the STA. So I put

th.SetApartmentState(ApartmentState.STA);

After few issues, I managed to make the thread work but now I have

Thread was in an invalid state for the operation being executed.

This is my Thread function :

public static void ouvrir(object name)
{
    saveFileDialog1.Filter = "Microsoft Word Document (.docx)|*.docx";
    saveFileDialog1.RestoreDirectory = true;
    saveFileDialog1.Title = "Where to save the " + (string)name + " ? ";
    DialogResult result = saveFileDialog1.ShowDialog();
    oke = true;
    try
    {
        if (result == DialogResult.OK)
        {
            boule = true;
            ptth = saveFileDialog1.FileName;
        }
    }
    catch (Exception exc)
    { MessageBox.Show(exc.Message); }
}

This line thows the error (I save a word doc using interop):

doc.SaveAs(imp);

I googled the error but it seems that i'm the only one on earth to have this issue... This is way out of my understanding, I sail in an ocean of doubt and ignorance.

Thank you

  • 1
    Are you calling the save dialog from a background thread? – bashrc Apr 27 '16 at 08:02
  • I don't think so, I tried with the new thread, and then I tried with the new thread abort and neither worked – UnderPaidIntern Apr 27 '16 at 08:04
  • If you find yourself creating a new thread and setting it's apartment state, it's usually an indication that you're starting from the wrong place. I.e. what *type* of application are you writing this code in? Is it actually a Forms/WPF application? – Damien_The_Unbeliever Apr 27 '16 at 08:10
  • Win Forms ! Yeah, the problem is that I really need to make it work and a new thread seems to be the only way to prevent the crash :/ – UnderPaidIntern Apr 27 '16 at 08:13
  • You must set the appartment state before starting the thread. i.e. `var th = new Thread(...); th.SetAppartmentState(AppartmentState.STA); th.Start();` – adrianm Apr 27 '16 at 08:53

1 Answers1

1

You must show the dialog on the same thread you used to create the form, so you shouldn't use a thread here at all. If you need to invoke the save dialog from a worker thread, use the Invoke method to execute the code on the UI thread.

Example: How to update the GUI from another thread in C#?

Here's some more information from MSDN: Control.InvokeRequired Property

Community
  • 1
  • 1
EventHorizon
  • 2,916
  • 22
  • 30
  • Thanks Drag, actually I tried to just put _var thread = new Func(() => { return Thread.CurrentThread; }).Invoke(); thread.SetApartmentState(ApartmentState.STA);_ But it doesn't seem to solve the crash.. Can it come from another thing? Because the app works in every computer except one, where the crash occures. – UnderPaidIntern Apr 27 '16 at 08:16
  • It's Dag, not Drag :). Not sure what you're trying to do here or why you're changing the apartment type? All UI operations should run on the main thread, you should only use worker threads for background tasks like calculations, downloads, database access etc. – EventHorizon Apr 27 '16 at 08:19
  • I'm trying to fix the crash when the saveFileDialog opens. To do this everyone told me to put it in another thread. But when I do, I have the error "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.". So I put it in STA, and then I had this error "Thread was in an invalid state for the operation being executed." Do you understand now? – UnderPaidIntern Apr 28 '16 at 05:25