【发布时间】:2010-07-19 07:45:17
【问题描述】:
我有 2 个表单 ParentForm 和一个子表单。在我的父表单中,我有一个线程侦听器,它侦听更新 ParentForm 区域的提要。现在,我有一个 ChildForm,它还需要将来自侦听器的数据放置在 ChildForm 的一个区域上。线程侦听器在获取提要时使用委托来更新我的 ParentForm。
我的 ParentForm 有这些。
private delegate void UpdateLogCallback(string strFeed);
private Thread thr;
private void InitializeFeed()
{
...
// Get the feed connection
...
thr = new Thread(new ThreadStart(ReceivedFeeds));
thr.Start();
}
private void ReceivedFeeds()
{
string strFeed = GetFromStream();
// invoke my updater while connected
while(Connected)
{
this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { strFeed });
}
}
private void UpdateLog(string strFeed)
{
txtLog.AppendText(strFeed + "\r\n");
}
这很好用,现在问题来了。当我从 ParentForm 打开 ChildForm 时,我还想使用从 ParentForm 中的 ReceivedFeeds() 获得的内容更新该表单的一部分,我将如何实现这一点?我无法在 ChildForm 中创建另一个提要连接,因为这会复制连接并导致错误。我只想和UpdateLog() 在 ChildForm 中做的一样。
编辑
我正在调用 ChildForm 在父表单上的 OnClick 事件上打开并显示它。
// onclick event
ChildForm childForm = new ChildForm();
childForm.Name = ((ListBox)sender).SelectedItem.ToString();
childForm.ShowDialog(this);
这是我打开 ChildForm 的方式,以及如何在 UpdateLogCallback 或 UpdateLog() 中调用 ChildForm 中的方法
我的 ChildForm 中还有一个 UpdateLog() 方法。
【问题讨论】:
标签: c# winforms delegates multithreading