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>