共計(jì) 419 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Winform 中,如果需要跨線程關(guān)閉窗體,可以使用以下方法:
- 使用委托:定義一個(gè)委托類型,然后在需要關(guān)閉窗體的地方創(chuàng)建一個(gè)委托實(shí)例并調(diào)用窗體的 Close 方法。
public delegate void CloseFormDelegate();
public void CloseForm()
{if (this.InvokeRequired)
{this.Invoke(new CloseFormDelegate(CloseForm));
}
else
{this.Close();}
}
- 使用 BeginInvoke 方法:在需要關(guān)閉窗體的地方使用 BeginInvoke 方法異步調(diào)用窗體的 Close 方法。
if (this.InvokeRequired)
{this.BeginInvoke(new Action(() => this.Close()));
}
else
{this.Close();}
無(wú)論使用哪種方法,都可以確保在跨線程時(shí)安全地關(guān)閉窗體。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完
發(fā)表至: 網(wǎng)站制作
2024-05-27