質問

I was struggling finding a good title as I already solved my problem. I just need an explication of how I solved it ... because I am diving into stuff I just discovered today and it may not be clear right now for me.

So in order to resume my problem resolution here is the way I solved my problem, and what I do not understand.

I am designing a WindowsForm application that crawl into websites in order to retrieve some data. I have several different website to crawl in and each are different. The basic way I use is to use WebRequest. However one website forces me to simulate click, so for this specific website i use a WebBrowser control to invoke a specific javascript function.

I decided to get out of the main thread some crawling work as it was really time consuming and freezed my application.

The way I used to do this was a BackgroundWorker per Search method (one for each website). It worked well for every website except the one with the WebBrowser. I was getting an exception : "Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported.".

I found out on the web that I couldn't use the WebBrowser control as I was not in the thread where the WebBrowser was constructed. I decided to create a Form wrapping a WebBrowser inside the DoWork method of the BackgroundWorker instead of the original Form, and to call the Navigate() method for this WebBrowser.

It didn't work either, with the same exception. I found this answer (http://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread) which helped me a lot (I changed a little bit the answer piece of code to get it working as it was question oriented)

So right now :

I click a button, it creates a BackgroundWorker. Inside the BackgroundWorker, the launchSearch method is called. Within this launchSearch method, I create a new thread which is doing all the work I need (This thread has been created pretty much in the same way as the solution I linked above), and i set a "hasActiveThread" boolean to true while the thread has not finished working. When the work is finished, I get the result and release my BackgroundWorker.

What bother me is that i do not really understand why creating a Form inside my BackgroundWorker "DoWork" method was not working properly when trying to navigate to an url. If you could at least help me to understand why the solution has to be that "complicated", and the reasons behind this, i would be really thanksfull !

Al_th

役に立ちましたか?

解決

Your solution wasn't worked because all new threads running by BackgroundWorked was started without running Message Loop. Any user interaction actions works through Windows Messages mechanism. See http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows and http://msdn.microsoft.com/en-us/library/windows/desktop/ms644928%28v=vs.85%29.aspx for details. Simple creating of Form object doesn't init message loop. For running it you may call Form.Show or Form.ShowDialog methods, or Application.Run(...) methods. In working example Message Loop was started with Application.Run() method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top