这quetion是在延续我在this link问题。
我写了一个应用程序来比较在那里使用的方法和其他方法。在调试模式下运行应用程序时,出现错误“调用或BeginInvoke无法在控件上调用,直到窗口句柄被创建。”在UpdateCustDetails方法的第一个BeginInvoke中。尽管在没有调试的情况下运行代码时不会产生任何运行时错误。有任何想法吗??
谢谢, Abhi。
下面是我的代码: -
public delegate void UpdateLabelDelegate(Label lb, string text);
public delegate void loadCustomersDelegate();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
loadCustomersDelegate del = new loadCustomersDelegate(UpdateCustDetails);
IAsyncResult ar = del.BeginInvoke(null, null);
while (!ar.IsCompleted)
{
}
}
public void updateLabel(Label lb, string text)
{
lb.Text = text;
}
public void UpdateCustDetails()
{
BeginInvoke(new UpdateLabelDelegate(updateLabel), new object[] { label1, "Test" });
BeginInvoke(new UpdateLabelDelegate(updateLabel), new object[] { label2, "Test1234" });
BeginInvoke(new UpdateLabelDelegate(updateLabel), new object[] { label3, "Test5678" });
BeginInvoke(new UpdateLabelDelegate(updateLabel), new object[] { label4, "Test0000" });
}
}
@ 366436 ABHI你在哪里住在印度 – 2010-08-06 08:22:33
史蒂芬你好,我是印度裔,但我总部设在英国目前。 – 2010-08-06 08:31:48
尝试将代码从Form1构造移动到为onload事件的形式的。 (除了导致InitializeComponents-调用)。 – Alxandr 2010-08-06 08:41:03