Solution to threadstateexception in form-free mode

Source: Internet
Author: User

Symptom: "Before you can call Ole, you must set the current thread to single-threaded unit (STA) mode. Make sure that your main function has the stathreadattribute flag. This exception is thrown only when the debugger is attached to the process"

 

In non-form mode, the above problem occurs when openfiledialog. showdialog () is used. Google uses the following two methods:

1,[Stathread] <br/> static void main ()

2,Thread APP = new thread (New parameterizedthreadstart (showwindow); app. apartmentstate = apartmentstate. sta; </P> <p> // You can also <br/> thread newthread = new thread (New threadstart (threadmethod); newthread. setapartmentstate (apartmentstate. MTA );

 

But no..

 

Therefore, Google is used. .

 

The solution code is as follows:

Private dialogresult stashowdialog (filedialog DIALOG) <br/>{</P> <p> dialogstate state = new dialogstate (); </P> <p> state. dialog = dialog; </P> <p> system. threading. thread t = new system. threading. thread (state. threadprocshowdialog); </P> <p> T. setapartmentstate (system. threading. apartmentstate. sta); <br/> T. start (); <br/> T. join (); <br/> return state. result; <br/>}</P> <p> public class dialogstate <br/>{< br/> Public dialogresult result; <br/> Public filedialog dialog; </P> <p> Public void threadprocshowdialog () <br/>{< br/> result = dialog. showdialog (); <br/>}< br/> call time: </P> <p> openfiledialog openfiledlg = new openfiledialog (); </P> <p> openfiledlg. initializelifetimeservice (); </P> <p> openfiledlg. filter = "config files (*. config) | *. config | (*. XML) | *. XML "; </P> <p> openfiledlg. title = "select Configuration File"; </P> <p> dialogresult ret = stashowdialog (openfiledlg); </P> <p> If (ret = dialogresult. OK) </P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.