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. problem with calling folderbrowingDialog in a child thread.

problem with calling folderbrowingDialog in a child thread.

Scheduled Pinned Locked Moved C#
comdebugginghelp
6 Posts 4 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.
  • P Offline
    P Offline
    prasadbuddhika
    wrote on last edited by
    #1

    in a child thread i'm calling folderbrowsing dialog , i get the exceptiuon and , i got some articles saying i must use STAThreadAttribute attribute for the main method , i did that and the result was the same "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." i tried

    inthrd.SetApartmentState(System.Threading.ApartmentState.STA);

    too but still the same result. can any one have any idea about this. thanx in advance.

    L L 2 Replies Last reply
    0
    • P prasadbuddhika

      in a child thread i'm calling folderbrowsing dialog , i get the exceptiuon and , i got some articles saying i must use STAThreadAttribute attribute for the main method , i did that and the result was the same "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." i tried

      inthrd.SetApartmentState(System.Threading.ApartmentState.STA);

      too but still the same result. can any one have any idea about this. thanx in advance.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I don't know all the details, however here are some facts: 1. you can set the apartment state only once; 2. IIRC you can't set it at all on ThreadPool threads; 3. I don't recall what happens if you try and ignore #1 or #2. 4. the easiest way to solve such problems most often is to do all user interaction on the main (aka GUI) thread. BTW: when referring to some error message or exception you should paste it verbatim in your question. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
      Please use <PRE> tags for code snippets, they improve readability.
      CP Vanity has been updated to V2.3

      P 1 Reply Last reply
      0
      • L Luc Pattyn

        I don't know all the details, however here are some facts: 1. you can set the apartment state only once; 2. IIRC you can't set it at all on ThreadPool threads; 3. I don't recall what happens if you try and ignore #1 or #2. 4. the easiest way to solve such problems most often is to do all user interaction on the main (aka GUI) thread. BTW: when referring to some error message or exception you should paste it verbatim in your question. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        P Offline
        P Offline
        prasadbuddhika
        wrote on last edited by
        #3

        thanks,actually what i'm doing is creating a new window in the child thread, so in that window i want to open this folderbrowsingdialog, so at that point i get the exception. and following is the exception i got 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. thanks in advance.

        P B 2 Replies Last reply
        0
        • P prasadbuddhika

          thanks,actually what i'm doing is creating a new window in the child thread, so in that window i want to open this folderbrowsingdialog, so at that point i get the exception. and following is the exception i got 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. thanks in advance.

          P Offline
          P Offline
          prasadbuddhika
          wrote on last edited by
          #4

          thanks again . i found the issue , i'm using a separate class for creating threads in the application , and i had set the

          l_thrd.SetApartmentState(ApartmentState.STA);

          out side that class , when i moved this line into that thread creating class i got my problem solved. but i really don't know difference the two places i used the code thankx .

          1 Reply Last reply
          0
          • P prasadbuddhika

            thanks,actually what i'm doing is creating a new window in the child thread, so in that window i want to open this folderbrowsingdialog, so at that point i get the exception. and following is the exception i got 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. thanks in advance.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            It is generally a bad idea to create UI on child threads, particularly if those threads are doing any data processing. The modal stack for different threads is separated, so (for example) message boxes and dialogs from a child thread won't interact as you expect with the main thread. A modal dialog is better than normal forms because you don't need to add a message pump to the thread but it's still not recommended in most situations. (For example, what if two threads want to ask the same question at the same time?) Have you thought about why you want to do this?

            1 Reply Last reply
            0
            • P prasadbuddhika

              in a child thread i'm calling folderbrowsing dialog , i get the exceptiuon and , i got some articles saying i must use STAThreadAttribute attribute for the main method , i did that and the result was the same "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." i tried

              inthrd.SetApartmentState(System.Threading.ApartmentState.STA);

              too but still the same result. can any one have any idea about this. thanx in advance.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It is always a best practise to limit your user interaction to the main GUI thread. Modal windows are modal only to the thread they're created on and not modal to the entire process as one would expect.

              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