-2

Am getting" 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" error. Here is the following code.

if (externalButton.Checked == true)
{
    // int i = 1;
    saveFileDialog.Title = "Save the Proofer Report";
    saveFileDialog.Filter = "Document Files (*.doc)|*.doc|Document Files        (*.docx)|*.docx";
     saveFileDialog.FilterIndex = 0;
     saveFileDialog.InitialDirectory = "MyDocuments";
     saveFileDialog.FileName = "Proofer Report -- " +  Path.GetFileName((string)fileName) + ".doc";
     //i.tostring()
     saveFileDialog.DefaultExt = ".doc"; 

     saveFileDialog.ShowHelp = true;
     saveFileDialog.ShowDialog();-----getting the error here
     fname = saveFileDialog.FileName;
  }
  else
  {
     fname =(string)fileName;              
  }
  if (fname != "")
  {               
     if (worker.CancellationPending == true)
     {
        // report progress
        worker.ReportProgress(25);
        return;

}

Program.cs

     [STAthread]
static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

    }
user1665707
  • 565
  • 3
  • 10
  • 24

2 Answers2

0

Ensure that your Main function has STAThreadAttribute marked on it.

Alternatively, if you are running your UI code on another thread, use some mechanism to invoke it on the main thread (e.g. BeginInvoke).

As for the technical reasons behind why the single-threaded apartment model are required for the save file dialog, it stems from the common file dialog using the Windows Shell. Third-party extensions can be loaded here, and they expect the single-threaded apartment thread model.

The Remarks section of ShellExecute has some good information about this requirement, although it's written for C++ developers.

ta.speot.is
  • 26,445
  • 8
  • 64
  • 94
-1

Solution 1

Use STAThread above your Main method.

[STAThread]
static void Main(string[] args)

{

}

Solution 2

If solution does not work, then clean your solution. Also cross check if all dlls are really deleted. Go to your debug folder and delete any old/stale dll from there. Then rebuilt your solution again and everything should be Ok now.

My experience says....most of the time, Solution 2 works fine.

Sandy
  • 10,930
  • 26
  • 73
  • 118
  • -1 code is on a background thread. COM threading model is set per thread. – ta.speot.is Feb 14 '13 at 09:40
  • i dont hv any dll in the debug folder – user1665707 Feb 14 '13 at 09:41
  • I asked to cross check...if any.try cleaning your solution – Sandy Feb 14 '13 at 09:41
  • Putting STAThreadAttribute on Main initializes COM for the main thread with the single-threaded apartment model. This has no effect on the threading model of other threads. – ta.speot.is Feb 14 '13 at 09:54
  • Okey.....I am not very comfortable with threads. And still I am not understanding where my concept goes wrong in my answer. When I run in this situation, most of the time I clean my debug folder and everything works fine. So just shared. Still, thanks for help. – Sandy Feb 14 '13 at 09:59