Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 6th, 2006, 02:32 AM
Friend of Wrox
 
Join Date: Jun 2003
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
======================================
 
Old June 1st, 2006, 04:29 PM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can get rid of the error message by creating a STA thread for your printing job as the error message indicates.

eg.

ThreadStart threadDelegate = new ThreadStart(ObjectName.MethodName);
Thread thread = new Thread(threadDelegate);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();


 
Old August 11th, 2006, 03:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi Veeru123,
Thanks for that reply, sorry I didn't respond earlier, have only just seen the response.
I gave up on this a while back and did as a windows app, but will now convert back to service.
Thanks very much again.
Rod

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old May 14th, 2007, 10:34 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,

I have the same exact problem. I am trying to create web service which prints html page without manual intervention. I tried using the thread suggested by veeru123. Now, I am not getting the exception, but its not printing. Its just running through the code.

Can you please send me the code in case if it worked for you.

Please reply me back in either case.

Thanks a lot.

Naveen.

 
Old May 7th, 2009, 04:48 AM
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Service and webrowser issue

Hi I am facing same probles and very upsate with this, now I could solved the probles of object creating but its not firing document complete event, any idea.

thanks in advance
 
Old March 22nd, 2010, 07:28 PM
sry sry is offline
Registered User
 
Join Date: Mar 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same problem

Has anyone had any luck with this? I'm having the exact same problem and cannot for the life of me figure out how to get past it.

Thanks a ton.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Impersonate with windows service for Service A/C vinod_yadav1919 C# 0 October 18th, 2008 02:29 PM
Windows service exarkuun General .NET 0 September 7th, 2007 10:19 AM
Threads In Windows XP selorm Windows Server 1 December 7th, 2006 11:10 PM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM
Problems with threads. Threads disappearing? DriesNeyrinck VB.NET 0 June 10th, 2003 08:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.