-1

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
  • 615
  • 3
  • 11
  • 24
  • 2
    What line is raising this error? – Shadow The GPT Wizard Feb 14 '13 at 09:00
  • Did you *Ensure that your Main function has STAThreadAttribute marked on it*? – ta.speot.is Feb 14 '13 at 09:11
  • Are you trying to show a Dialog on a background thread? – Simon Mourier Feb 14 '13 at 09:17
  • See the second point in my answer. Don't do UI stuff on the background thread. It's possible, but ... no. Your modal dialog will still pump window messages, and you can still run non-UI things on threads. – ta.speot.is Feb 14 '13 at 09:18
  • i din understand much...i used to follow the same while coding in web apps...so y this error...what do i do nw? – user1665707 Feb 14 '13 at 09:22
  • I don't understand why you would use a saveFileDialog in a web application. – ta.speot.is Feb 14 '13 at 09:30
  • This code is a mess...you've added in your `Main` method, great, where is it actually sitting in your code? ...currently you've got it sitting in an `if` in your example above. Structure your example above **exactly** how it is laid out in your solution. – Arran Feb 14 '13 at 09:33

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,914
  • 8
  • 68
  • 96
-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
  • 11,332
  • 27
  • 76
  • 122
  • -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