views:

818

answers:

1

How to programmatically control multiple browsers (aXWebBrowser control), from a single winforms app process, targeting the same remote website, but each browser living in its own session scope with the remote site?

Goals - Build an application which automates use of a website. The goal is for the application to do the work of up to 5 users interacting with a browser on the same web site.

Apparent Challenges - Each browser instance shares its “session” data sent to it by the remote website. The result is the various browsers are not able to act as actual multiple human users would. No matter how many different aXWebBrowser controls are instantiated, each loses its session context, and shares the session context established by the last / latest/ most recently instantiated browser. In other words the last launched control destroys the established session context of any control preceding it.

Have Already Tried - Adding one of the following registry keys to 'hkcu\software\microsoft\internet explorer\main': TabProcGrowth=DWORD:0, FrameMerging=DWORD:0, SessionMerging=DWORD:0. When launching IE8 from my desktop icon (outside the app) this works fine, IE8 behaves as desired. However, when running the application, using the axWewbBrowser controls, it does work, the registry settings seem to have no effect on the axWebBrowser controls. Other ways to see the disired behavior outside the application include: clicking "New Session" in IE8 File menu, and launching iexplore.exe with -nomerge. These do not work within the application because the axWebBrowser control uses Wininet for communication.

Constraints - There is considerable code already written and working using the aXWebBrowser Control (Internet Explorer ActiveX automatable Web Browser) so an ideal solution will not require the code to be re-written with a new control. - After the solution is found the application will surface the browser window(s) to the workstation user. - A winforms application (.NET 2.0) is hosting the controls - The browsers are all targeting the same remote website.

A: 

From what I have been able to tell, as long as each browser is being loaded on the same thread, IE is always going to treat them as the same session.

I got around this problem by creating a new thread and a new window for each session.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //// How many test windows do we need to create.
        int numberOfClients = 5;
        System.Threading.Thread[] threads = 
            new System.Threading.Thread[numberOfClients];

        //// Create threads for each of the windows, and then start them.
        for (int i = 0; i < numberOfClients; i++)
        {
            threads[i] = new System.Threading.Thread(Program.StartTest);
            threads[i].SetApartmentState(System.Threading.ApartmentState.STA);
            //// Passing in the startup parameters for each each instance.
            threads[i].Start(new StartupParameters(i));
        }

        //// This will keep the application running until 
        ////  all the windows are closed.
        foreach (System.Threading.Thread thread in threads)
        {
            thread.Join();
        }
    }

    /// <summary>
    /// Starts the test form.
    /// </summary>
    /// <param name="state">
    /// The state object containing our startup parameters.
    /// </param>
    private static void StartTest(object state)
    {
        StartupParameters parameters = state as StartupParameters;
        YourTestForm yourTestForm = new YourTestForm();

        //// Set the needed parameters before we run the form.  
        //// Add your parameters here.
        yourTestForm.Text = string.Format("Test form {0}", parameters.Index);

        //// Run the test.
        Application.Run(yourTestForm);
    }
}

/// <summary>
/// Contains the startup parameters used to configure 
/// each new instance of the test form.
/// </summary>
public class StartupParameters
{
    /// <summary>
    /// Initializes a new instance of the <see cref="StartupPramitures"/> class.
    /// </summary>
    /// <param name="index">The index.</param>
    public StartupParameters(int index)
    {
        this.Index = index;
    }

    /// <summary>
    /// The index for this test form.
    /// </summary>
    public int Index { get; private set; }
}
Adam L
I've tried your sample code (with IE8 installed) and all browser controls actually share the same session.
Vizu