multithreading - Windows Form UI Thread issue c# -
i have working application, except in one computer. in computer application crashes when savefiledialog opens. no errors, no exceptions.
after research found 1 way fix crash make savefiledialog in new thread.
static void open(object name) { savefiledialog savefiledialog1 = new savefiledialog(); savefiledialog1.filter = "microsoft word document (.docx)|*.docx"; savefiledialog1.filterindex = 2; savefiledialog1.restoredirectory = true; savefiledialog1.title = "where save " + (string)name + " ? "; try { th.isbackground = false; boule = savefiledialog1.showdialog() == dialogresult.ok; ptth = savefiledialog1.filename; } catch (exception exc) { messagebox.show(exc.message); } }
i put in event:
thread th = new thread(new parameterizedthreadstart(open)); th.start("quotation");
the problem have error
current thread must set single thread apartment (sta) mode before ole...
so checked , have "[stathread]" before main. added line in "open" method:
th.setapartmentstate(apartmentstate.sta);
but new error appear :
failed set specified com apartment state
what did wrong ? how make proper ui thread ? there easier way?
edit : it's more weird because in app use openfiledialog picture , works perfectly. issue savefiledialog? come else?
instead of starting new thread can set apartmentstate
current ui thread.
var thread = new func<thread>(() => { return thread.currentthread; }).invoke(); thread.setapartmentstate(apartmentstate.sta);
Comments
Post a Comment