簡體   English   中英

System.InvalidOperationException,在其他地方使用的對象

[英]System.InvalidOperationException, Object in use elsewhere

我在隨機時間收到InvalidOperationException。

public void abStart()
{ 
    try
    {
        AB ab = new AB(wb.getCookies(), side);
        Application.Run(ab); //this is where the exception is thrown
    }
    catch {  }
}

此方法在不同的線程上執行,如下所示:

if (abON[0] == null || !abON[0].IsAlive)
{
    abON[0] = new Thread(new ThreadStart(abStart));
    abON[0].SetApartmentState(ApartmentState.STA);
    abON[0].Start();
}

這應該有幫助

public void abStart()
    {
        try
        {
            AB ab = new AB(wb.getCookies(), side);
            this.Invoke((MethodInvoker)delegate
            {
            Application.Run(ab); //this is where the exception is thrown
            });
        }
        catch { }
    }

這段代碼對此進行了改進:

        try
        {
            this.Invoke((MethodInvoker)delegate
            {
                // handles the code on its own thread
                if (abON[0] == null || !abON[0].IsAlive)
                {
                    abON[0] = new Thread(new ThreadStart(abStart));
                    abON[0].SetApartmentState(ApartmentState.STA);
                    abON[0].Start();
                }
            });
        }
        catch (Exception ex)
        {
            MessageBox.Show("" + ex);
        }

原因是您面臨跨線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM