Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OpenFileDialog throws a Threading Exception

OpenFileDialog throws a Threading Exception

Scheduled Pinned Locked Moved C#
databasecomdebugginghelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jordanwb
    wrote on last edited by
    #1

    Okay what I'm trying to do is use the Dialog window to have the user find an Excel Database file (xls extension). I have searched Google, Msdn and the site. 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. Code where OpenFileDialog is created: private void import_db(object sender, EventArgs e) { OpenFileDialog search_for_file = new OpenFileDialog(); search_for_file.AddExtension = true; search_for_file.CheckFileExists = true; search_for_file.DefaultExt = "xls"; search_for_file.Multiselect = false; DialogResult result = search_for_file.ShowDialog(); string file_name = search_for_file.FileName; } It throws the error when I get to the ShowDialog method.

    M 1 Reply Last reply
    0
    • J Jordanwb

      Okay what I'm trying to do is use the Dialog window to have the user find an Excel Database file (xls extension). I have searched Google, Msdn and the site. 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. Code where OpenFileDialog is created: private void import_db(object sender, EventArgs e) { OpenFileDialog search_for_file = new OpenFileDialog(); search_for_file.AddExtension = true; search_for_file.CheckFileExists = true; search_for_file.DefaultExt = "xls"; search_for_file.Multiselect = false; DialogResult result = search_for_file.ShowDialog(); string file_name = search_for_file.FileName; } It throws the error when I get to the ShowDialog method.

      M Offline
      M Offline
      michaelvdnest
      wrote on last edited by
      #2

      Dumb question: Does you Main function have a STAThread attribute? [STAThread] public static void Main(string[] args) { ... }

      J 1 Reply Last reply
      0
      • M michaelvdnest

        Dumb question: Does you Main function have a STAThread attribute? [STAThread] public static void Main(string[] args) { ... }

        J Offline
        J Offline
        Jordanwb
        wrote on last edited by
        #3

        If you mean "Did I put [STAThread] before your main function?" then no. I assume that that will fix the problem. *Edit* Yes that did it, Thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups