multithreading - InvalidCastException when using webbrowser and threads in c# -
when trying use webbrowser component inside backgroundworker.dowork function , got exception :
system.invalidcastexception unhandled user code
http://i.stack.imgur.com/ejft3.jpg
here's code :
void m_oworker_dowork(object sender, doworkeventargs e) { //note : never play ui thread here... string line; //time consuming operation while ((line=sr.readline()) != null ){ int index = line.indexof(":"); htmldocument doc = web.document; thread.sleep(1000); m_oworker.reportprogress(cnt); //if cancel button pressed while execution in progress //change state cancellation ---> cancel'ed if (m_oworker.cancellationpending) { e.cancel = true; m_oworker.reportprogress(0); return; } cnt++; } //report 100% completion on operation completed m_oworker.reportprogress(100); }
or way use thread in c# ?
cause when use thread.sleep method in main gui freezes !!
the webbrowser doesn't being accessed other threads. try passing in runworkerasync() this:
private void button1_click(object sender, eventargs e) { htmldocument doc = web.document; m_oworker.runworkerasync(doc); } void m_oworker_dowork(object sender, doworkeventargs e) { htmldocument doc = (htmldocument)e.argument; //note : never play ui thread here... string line; //time consuming operation while ((line = sr.readline()) != null) { int index = line.indexof(":"); thread.sleep(1000); m_oworker.reportprogress(cnt); //if cancel button pressed while execution in progress //change state cancellation ---> cancel'ed if (m_oworker.cancellationpending) { e.cancel = true; m_oworker.reportprogress(0); return; } cnt++; } //report 100% completion on operation completed m_oworker.reportprogress(100); }
Comments
Post a Comment