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 8059503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:41:24+00:00 2026-06-05T09:41:24+00:00

First, what I need is – n WebBrowser-s, each in its own window doing

  • 0

First, what I need is – n WebBrowser-s, each in its own window doing its own job. The user should be able to see them all, or just one of them (or none), and to execute commands on each one. There is a main form, without a browser, this one contains control panel for my application.

The key feautre is, each browser logs on to secured web page and it needs to stay logged in as long as possible. Well, I’ve done it, but I’m afraid something is wrong with my approach.

The question is:

Is code below valid, or rather a nasty hack which can cause problems:

internal class SessionList : List<Session> {

    public SessionList(Server main) {
        MyRecords.ForEach(record => {
            var st = new System.Threading.Thread((data) => {
                var s = new Session(main, data as MyRecord);
                this.Add(s);
                Application.Run(s);
                Application.ExitThread();
            });
            st.SetApartmentState(System.Threading.ApartmentState.STA);
            st.Start(record);
        });
    }

    // some other uninteresting methods here...

}

What’s going on here? Session inherits from Form, so it creates a form, puts WebBrowser into it, and has methods to operate on websites. WebBrowser requires to be run in STA thread, so we provide one for each browser. The most interesting part of it is Application.Run(s). It makes the newly created forms alive and interactive. The next Application.ExitThread() is called after browser window is closed and its controls disposed. Main application stays alive to perform the rest of the cleanup job.

When user select “Exit” or “Shutdown” option – first the browser threads are ended, so Application.ExitThread() is called. It all works, but everywhere I can read about “main GUI thread” – and here – I’ve created many GUI threads. I handle communication between main form and my new forms (sessions) with thread-safe methods using Invoke(). It all works, so is it right or is it wrong?

Is everything right with using Application.Run() more than once in one application? 🙂 An ugly hack or a normal practice? This code dies if I start a WebBrowser from the session form thread. It beats me why. It works however if I start WebBrowser (by changing its Url property) from any other thread. I’d like to know more what is really happening in such application. But most of all – I’d like to know if my idea of “applications in application” is OK.

I’m not sure what exactly does Application.Run() do. Without it forms created in new threads were dead unresponsive. How is it possible I can call Application.Run() many times? It seems to do exactly what it should, but it seems a little undocumented feature to me. I’m almost sure, that the crashes are caused by WebBrowser component itself (since it’s not completely “managed” and “native”). But maybe it’s something else.

  • 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-06-05T09:41:26+00:00Added an answer on June 5, 2026 at 9:41 am

    Is everything right with using Application.Run() more than once in one application? 🙂 An ugly hack or a normal practice?

    Some of both 😉 This is perfectly acceptable, in that it will function as you are expecting, but it’s not exactly a “normal practice.”

    I’m not sure what exactly does Application.Run() do.

    Application.Run basically does a couple of things. First, it installs the property SynchronizationContext into the thread for Windows Forms to run properly. It then starts the windows message processing in that thread, which processes all messages from Windows going into the thread. This is what allows forms to work properly.

    There’s nothing particularly wrong with doing this, but it isn’t a standard practice. Given your design goals, I do question whether this would be better served by just launching a separate process instead of trying to run each operation within a separate thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i try to program a LdapAuthentication and i need some help. First i need
I'm programming in Microsoft VBA. At first I need to generate a QueryTable with
I'm using Entity Framework 4.3.1 Code-First and I need to split an entity between
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
I am climbing the java learning curve, and for the first time I need
First of all I need to say that I'm new to jQuery. When I
First of all: What I need to achieve is write an Xml document Header,
I need to fetch only first record (because I need last date) of resultset,
I need to change the first letter of every line in a file to
I need to get the first and last dimension of an numpy.ndarray of arbitrary

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.