Problem with a service using a WinForms control

G

Guest

Just as an exercise I'm trying to write a program that will periodically read
a Web page (not mine), get some information from it, and store the
information.

It seemed the most straightforward tactic was to write a service and have
that service use an instance of WebBrowser.

The problem is that I get the following error when the thread in which the
service runs tries to instantiate the WebBrowser:

The inner exception System.Threading.ThreadStateException was thrown with
the following error message: ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated
because the current thread is not in a single-threaded apartment.

So, I want to set the service's thread to STA, but I don't know how to tell
the SCM to start the thread that way. If it's possible, how?

Otherwise I can probably spawn off another thread in STA mode, but that
seems a bit wasteful. I'll try it anyway while awaiting responses.

Alternatively, how might I simply pull in a Web page without using a
WebBrowser?
 
K

Kevin Spencer

Use the System.Net.HttpWebRequest and System.Net.HttpWebResponse classes.

http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
http://msdn2.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

First, a Service does not have a user interface, and the WebBrowser is a UI
Control. Second, the WebBrowser is an ActiveX Control, which is COM, and not
directly compatible with .Net. COM Interop should be avoided whenever
possible for performance reasons.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top