View Single Post
 
Old April 6th, 2006, 02:32 AM
rodmcleay rodmcleay is offline
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Threads, WebBrowser and Windows Service error

Hi all,
Im trying to write a windows service to replace an existing VB6 app that uses a WebBrowser control to print a web page.
I am getting a thread exception error when instanciating the WebBrowser.
I have tried using the [STAThread] attribute on the main() function of the service program as my research has suggested but that does not make any difference.
Code:
[STAThread] 
        static void Main()
        {
            ServiceBase[] ServicesToRun;

            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            //   ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
            //
            ServicesToRun = new ServiceBase[] { new InternetPickups() };

            ServiceBase.Run(ServicesToRun);
        }

The line causing the error is
Code:
        private void setBrowser()
        {
            webBrowser = new WebBrowser();
        }
and I have also tried the STAThread attribute on that method.

The error is
 
Quote:
quote:Service cannot be started. System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Quote:
   at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
   at System.Windows.Forms.WebBrowser..ctor()
   at InternetPickupsWS.InternetPickups.setBrowser() in C:\Documents and Settings\Rod McLeay\My Documents\My Clients\CTC\InternetPickupsUtility\SourceCode\Inte rnetPickupsWS\InternetPickupsWS\InternetPickups.cs :line 31
Also, any other suggestions as to how to load and print a web page to the default printer without user interaction from a windows service would be equally as good for my purposes.

Any assistance would greatly be appreciated.
Thanks for your time.
Rod



======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================