COMException问题

时间:2010-12-27 10:58:28

标签: c# .net com

想知道是否有人可以帮我解决问题。下面是代码,在代码之后是对异常抛出位置的探索。

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();
    }
}

}

此行引发异常:

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

但是,当它到达这一行时它会被抛出:

captcha.ShowDialog();

它登录,并填写应用程序详细信息和Form2加载正常,但一旦加载,大约2-3秒后异常发生。我想知道这是否与线程有关?但如果是的话,我不知道如何解决它。

抛出的完整异常是:

The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))

1 个答案:

答案 0 :(得分:0)

得到了!由于你的线程,在Windows开发中,微软不建议通过线程访问UI。如果确实需要,请使用互斥锁来避免两个或多个线程同时访问同一个UI元素。