Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:08:28+00:00 2026-05-23T18:08:28+00:00

I want to make 3 threads that each run the WebBroswer control. So I

  • 0

I want to make 3 threads that each run the WebBroswer control. So I would like to use the ThreadPool to make things easy.

for(int i = 0;i < 3;i++)
{
   ThreadPool.QueueUserWorkItem(new WaitCallback(gotoWork), i));
}
WaitAll(waitHandles);

……/

void gotoWork(object o)
{
   string url = String.Empty;
   int num = (int)o;
   switch(num)
   {
     case 0:
       url = "google.com";
       break;
     case 1:
       url = "yahoo.com";
       break;
     case 2:
       url = "bing.com";
       break;
   }
   WebBrowser w = new WebBrower();
   w.Navigate(url);
}

But I get an error saying that I need a STA thread which the ThreadPool will never be. Before trying this method I tried this.

Thread[] threads = Thread[3];
for(int i = 0;i < 3;i++)
{
    threads[i] = new Thread(new ParameterizedStart(gotoWork);
    threads[i] = SetApartmentState(ApartmentState.STA); //whoo hoo
    threads[i] = Start();
}
for(int i = 0; i < 3;i++)
{
    threads[i].Join();
}

And the WebBrowsers all initialized and everything looks good but only one more two will actually do anything and its never consistant at all. Threading has been such a nightmare. Can anybody suggest a nice alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:08:28+00:00Added an answer on May 23, 2026 at 6:08 pm
    public sealed class SiteHelper : Form
    {
        public WebBrowser mBrowser = new WebBrowser();
        ManualResetEvent mStart;
        public event CompletedCallback Completed;
        public SiteHelper(ManualResetEvent start)
        {
            mBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(mBrowser_DocumentCompleted);
            mStart = start;
        }
        void mBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Generated completed event
            Completed(mBrowser);
        }
        public void Navigate(string url)
        {
            // Start navigating
            this.BeginInvoke(new Action(() => mBrowser.Navigate(url)));
        }
        public void Terminate()
        {
            // Shutdown form and message loop
            this.Invoke(new Action(() => this.Close()));
        }
        protected override void SetVisibleCore(bool value)
        {
            if (!IsHandleCreated)
            {
                // First-time init, create handle and wait for message pump to run
                this.CreateHandle();
                this.BeginInvoke(new Action(() => mStart.Set()));
            }
            // Keep form hidden
            value = false;
            base.SetVisibleCore(value);
        }
    }
    

    An other class as

    public abstract class SiteManager : IDisposable
    {
        private ManualResetEvent mStart;
        private SiteHelper mSyncProvider;
        public event CompletedCallback Completed;
    
        public SiteManager()
        {
            // Start the thread, wait for it to initialize
            mStart = new ManualResetEvent(false);
            Thread t = new Thread(startPump);
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();
            mStart.WaitOne();
        }
        public void Dispose()
        {
            // Shutdown message loop and thread
            mSyncProvider.Terminate();
        }
        public void Navigate(string url)
        {
            // Start navigating to a URL
            mSyncProvider.Navigate(url);
        }
        public void mSyncProvider_Completed(WebBrowser wb)
        {
            // Navigation completed, raise event
              CompletedCallback handler = Completed;
              if (handler != null)
              {
                  handler(wb);
              }
        }
        private void startPump()
        {
            // Start the message loop
            mSyncProvider = new SiteHelper(mStart);
            mSyncProvider.Completed += mSyncProvider_Completed;
            Application.Run(mSyncProvider);
        }
    }
    
    
    class Tester :SiteManager
    {
        public Tester()
        {
            SiteEventArgs ar = new SiteEventArgs("MeSite");
    
            base.Completed += new CompletedCallback(Tester_Completed);
        }
    
        void Tester_Completed(WebBrowser wb)
        {
            MessageBox.Show("Tester");
            if( wb.DocumentTitle == "Hi")
    
            base.mSyncProvider_Completed(wb);
        }
    
        //protected override void mSyncProvider_Completed(WebBrowser wb)
        //{
        //  //  MessageBox.Show("overload Tester");
        //    //base.mSyncProvider_Completed(wb, ar);
        //}
    }
    

    On your main form:

    private void button1_Click(object sender, EventArgs e)
    {
        //Tester pump = new Tester();
        //pump.Completed += new CompletedCallback(pump_Completed);
        //pump.Navigate("www.cnn.com");
    
        Tester pump2 = new Tester();
        pump2.Completed += new CompletedCallback(pump_Completed);
        pump2.Navigate("www.google.com");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use case is simple: I want to run some boiler plate code before each
I want to make one thread that puts values to a queue when it
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make an entity that has an autogenerated primary key, but also
I've managed to make a typewriter class that does what I want it to
I have a single-threaded linux app which I would like to make parallel. It
I would like to know more about the extent it is possible to control
I want make datetimepicker in my project. Using jquery how it is possible? I
Let's say I want make some of my sources publicly available via my blog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.