【发布时间】:2010-10-17 16:13:43
【问题描述】:
我对线程的概念以及如何使用它们感到困惑。
我正在尝试编写一个相当基本的聊天程序(作为更大程序的一部分),它目前的工作方式如下:
“NetworkSession”类在循环中的单独线程上接收来自服务器的输入。如果它收到指示它应该打开一个新的聊天窗口的输入,它会构造一个新的 WPF 类 (ChatWindow) 并显示它。
最初我得到的错误是“调用线程必须是 STA,因为许多 UI 组件都需要这个。”。所以我将线程设置为 STA,但现在 WPF 表单当然无法使用,因为它与阻塞循环在同一线程上运行。
所以我的问题是如何从另一个线程中创建 WPF 表单的新实例。
我已经看到很多关于这个的讨论,但它倾向于处理从已经构建的表单运行委托。
这是一些代码。
while (Connected) //this loop is running on its own thread
{
Resp = srReceiver.ReadLine();
if (Resp.StartsWith("PING")) SendToServer("PONG");
if (Resp.StartsWith("CHAT FROM"))
{
String[] split = Resp.Split(' ');
Console.WriteLine("Incoming Chat from {0}", split[2]);
bool found = false;
if (Chats.Count != 0)
{
foreach (ChatWindow cw in Chats)
{
if (cw.User == split[2])
{
found = true;
cw.AddLine(cw.User, split[3]); // a function that adds a line to the current chat
}
}
}
if (!found)
{
ChatWindow temp = new ChatWindow(split[2], split[3]);
Chats.Add(temp); //this is a collection with T = ChatWindow
temp.Show();
}
}
}
【问题讨论】:
标签: c# wpf multithreading