想在子线程里操作界面空间,但是总是实现不了。请问如何开启线程使其显示??试了几位朋友说的方法,总是有错误;
(错误提示在下面,此为ASP.NET界面),请高手给以指点:
1.相关源代码如下:
public void Button1_Click(object sender, EventArgs e)
{ IpScan(); //这样可以显示;
//Thread t = new Thread(new ThreadStart(IpScan)); //开启线程就不可以;
//t.Start();
}
public void IpScan()
{
string msg = string.Empty; ——————
——————
if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
{
msg+= "The IP: " + pram_ipadd + "\r\n";
}
TextBox1.Text = msg; //将结果显示在文本框中;
}
采用的解决办法是:
public delegate void RefreshDlg(string msg);//先定义一个代理
................
................//此为上面源代码; if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
{
msg+= "The IP: " + pram_ipadd + "\r\n";
}
SetText(msg);//将结果显示在文本框中;
}public void SetText(string str)
{
if (this.textBox1.InvokeRequired)
{
RefreshDlg refresh = new RefreshDlg(SetText);
this.Invoke(refresh, new string[] { str });
}
else
{
this.textBox1.Text = str;
}
}
但提示如下错误:
以下分别提示:
'System.Web.UI.WebControls.TextBox' does not contain a definition for 'InvokeRequired' 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Invoke'
(错误提示在下面,此为ASP.NET界面),请高手给以指点:
1.相关源代码如下:
public void Button1_Click(object sender, EventArgs e)
{ IpScan(); //这样可以显示;
//Thread t = new Thread(new ThreadStart(IpScan)); //开启线程就不可以;
//t.Start();
}
public void IpScan()
{
string msg = string.Empty; ——————
——————
if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
{
msg+= "The IP: " + pram_ipadd + "\r\n";
}
TextBox1.Text = msg; //将结果显示在文本框中;
}
采用的解决办法是:
public delegate void RefreshDlg(string msg);//先定义一个代理
................
................//此为上面源代码; if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
{
msg+= "The IP: " + pram_ipadd + "\r\n";
}
SetText(msg);//将结果显示在文本框中;
}public void SetText(string str)
{
if (this.textBox1.InvokeRequired)
{
RefreshDlg refresh = new RefreshDlg(SetText);
this.Invoke(refresh, new string[] { str });
}
else
{
this.textBox1.Text = str;
}
}
但提示如下错误:
以下分别提示:
'System.Web.UI.WebControls.TextBox' does not contain a definition for 'InvokeRequired' 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Invoke'
public delegate void RefreshDlg(string msg);//先定义一个代理
................
................//此为上面源代码; if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
{
msg+= "The IP: " + pram_ipadd + "\r\n";
}
SetText(msg);//将结果显示在文本框中;
} public void SetText(string str)
{
if (this.textBox1.InvokeRequired)
{
RefreshDlg refresh = new RefreshDlg(SetText);
this.Invoke(refresh, new string[] { str });
}
else
{
this.textBox1.Text = str;
}
}
但提示如下错误:
以下分别提示:
'System.Web.UI.WebControls.TextBox' does not contain a definition for 'InvokeRequired' 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Invoke'
自己调自己?????????????Thread t=null;public void Button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(IpScan));
t.Start();
}
public void IpScan()
{
string msg = string.Empty;
if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
msg+= "The IP: " + pram_ipadd + "\r\n";
SetText(msg); //将结果显示在文本框中;
} void DoSetText(string str)
{
this.textBox1.Text = str;
}public void SetText(string str)
{
if (this.textBox1.InvokeRequired)
{
RefreshDlg refresh = new RefreshDlg(DoSetText);
this.Invoke(refresh, new string[] { str });
}
else
this.textBox1.Text = str;
}
也不对
this.Invoke(refresh, new Object[] { str });
Thread t=null;public void Button1_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(IpScan));
t.Start();
}
public void IpScan()
{
string msg = string.Empty;
if (pram_ipadd != "" && pram_ipadd !=null && fanhui_ipadd !=""&&fanhui_ipadd !=null)
msg+= "The IP: " + pram_ipadd + "\r\n";
SetText(msg); //将结果显示在文本框中;
} void DoSetText(TextBox TB, string str)
{
TB.Text = str;
}public void SetText(string str)
{
if (this.textBox1.InvokeRequired)
{
RefreshDlg refresh = new RefreshDlg(DoSetText);
textBox1.Invoke(refresh, new Object[] {textBox1, str });
}
else
this.textBox1.Text = str;
} 也可以
我试了上面的还是不行,错误还是原来的错误!!!!!!