0

Wondering if anyone could help with my problem. Below is the code, and after the code an explination of where the exception is thrown.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using WatiN.Core;
using System.Threading;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void button1_Click(object sender, EventArgs e)
    {
        Thread t = new Thread(createApplications);
        Settings.AutoStartDialogWatcher = false;
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.Start();
    }

    private void createApplications()
    {
        createApp("username", "password", "Test App", "This is just a test description", "http:/mysite.com");
    }

    private void createApp(String username, String password, String appName, String description, String appUrl) {
        var currentBrowser = new IE("http://mysite.com/login/php");

        currentBrowser.TextField(Find.ById("username")).TypeText(username);
        currentBrowser.TextField(Find.ById("password")).TypeText(password);
        currentBrowser.Button(Find.ById("submit")).Click();

        currentBrowser.GoTo("http://mysite.com/createmusicapp.php");

        currentBrowser.TextField(Find.ById("application_name")).TypeText(appName);
        currentBrowser.TextField(Find.ById("application_description")).TypeText(description);
        currentBrowser.TextField(Find.ById("application_url")).TypeText(appUrl);

        currentBrowser.RadioButton(Find.ById("client_application_desktop_1")).Click();
        currentBrowser.RadioButton(Find.ById("client_application_is_writable_1")).Click();

        WatiN.Core.Image captchaImage = currentBrowser.Div(Find.ById("recaptcha_image")).Image(Find.ByStyle("display", "block"));

        Form2 captcha = new Form2(captchaImage.Src);
        captcha.ShowDialog();
    }
}

}

The exception is thrown on this line:

currentBrowser.TextField(Find.ById("username")).TypeText(username);

BUT, it's thrown when it gets to this line:

captcha.ShowDialog();

It logs in, and fills in the app details and Form2 loads fine, but once loaded, after around 2-3 seconds the exception happens. I am wondering if it's anything to do with the threads? But I wouldn't know how to solve it if it was.

The complete exception thrown is:

The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
Jack Harvin
  • 6,605
  • 7
  • 23
  • 21
  • "I am wondering if it's anything to do with the threads?" - Does it work if you call `createApplications` directly from the main UI thread? – Dirk Vollmar Dec 27 '10 at 11:04
  • Yes it does, I don't get any exception when I call it from the main thread. – Jack Harvin Dec 27 '10 at 11:16
  • It would be ideal if I could call the captcha dialog within the tread but create it on the main. I've noticed a lot of apps work this way where they lock the main GUI whilst another dialog is open. – Jack Harvin Dec 27 '10 at 11:59

1 Answers1

0

Got! Because of your thread, in windows development, microsoft doesn't suggest access UI by thread. If you really need, use mutex to avoid two or more threads accessing the same UI elemnt at the same time.

erinus
  • 734
  • 4
  • 5
  • Glad my thread helped bud. I'm still stuck. Do you have any resources on the web that could help me? I've only just managed to understand threads. – Jack Harvin Dec 27 '10 at 11:57
  • look this page. It talks how to access UI from thread. http://stackoverflow.com/questions/2183287/backgroundworker-multithread-access-to-form – erinus Dec 27 '10 at 11:59