Home All Groups Group Topic Archive Search About

Problem with a service using a WinForms control

Author
11 Sep 2006 7:09 PM
PIEBALD
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?

Author
11 Sep 2006 8:50 PM
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.

Show quote
"PIEBALD" <PIEB***@discussions.microsoft.com> wrote in message
news:568641EC-B384-47DA-9D20-5F2C89B84EEA@microsoft.com...
> 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?

AddThis Social Bookmark Button