110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Windows.Forms;
namespace ASyncUpdateUI
{
public partial class Form1 : Form, FormInterface
{
public Form1()
{
InitializeComponent();
}
void FormInterface.SendResult(string text)
{
// 调用返回的数据.
// 在这里更新UI,更新不到.
// 但是可以弹出.
MessageBox.Show(text);
// !!!!!!
// 下面这一句设置失败
// TODO
this.targetText.Text = text;
}
private void send_Click(object sender, EventArgs e)
{
SendHTTP send = new SendHTTP();
send.SendGet("http://www.baidu.com", new Form1());
}
}
}
namespace ASyncUpdateUI
{
interface FormInterface
{
void SendResult(string text);
}
}
using System;
namespace ASyncUpdateUI
{
class SendHTTP
{
public void SendGet(string url,FormInterface formInterface)
{
// 模拟异步网络请求.
// .......................
// 返回网络请求结果.
//
string result = "ok";
formInterface.SendResult(result);
}
}
}
void FormInterface.SendResult(string text)
{
// 调用返回的数据.
// 在这里更新UI,更新不到.
// 但是可以弹出.
MessageBox.Show(text);
// !!!!!!
// 下面这一句设置失败
// 从这怎么才能成功设置值到文本框中?
this.targetText.Text = text;
}
void FormInterface.SendResult(string text)
{
// 调用返回的数据.
// 在这里更新UI,更新不到.
// 但是可以弹出.
MessageBox.Show(text);
// !!!!!!
// 下面这一句设置失败
//
// this.targetText.Text = text;
this.BeginInvoke(new Action(() =>
{
this.targetText.Text = text;
}));
}
上面这样写,报错,怎么处理啊,谢谢:
System.InvalidOperationException:“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。”