我正在研究有关C#WPF程序的论文,但我遇到了一个我不明白的错误。线程必须是STA线程,但它已经是
某处在我的主窗口代码即时通讯开始一个新的主题是这样的:
Thread searchServer = new Thread(new ThreadStart(doSearchServer));
searchServer.SetApartmentState(ApartmentState.STA);
searchServer.Start();
的doSearchServer方法执行以下操作:
private void doSearchServer()
{
bool connected = ServerConnection.authentication();
ServerConnection.getDeviceList();
gotDataFromServer = connected;
if (connected)
..
..
}
ServerConnection类是静态的,因为我还需要在类一些其他的Windows。
在ServerConnection.authentication()客户端(我的程序)尝试在我的服务器上进行身份验证。如果需要密码,我想打开一个新的PasswordWindow,你可以在这里看到:
public static bool authentication()
{
UdpClient client = new UdpClient();
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 55042);
IPEndPoint ipRec = new IPEndPoint(IPAddress.Any, 0);
byte[] bytes = Encoding.UTF8.GetBytes("authent|Username|Windows");
client.Send(bytes, bytes.Length, ip);
//Receive Answer
byte[] recBuffer = client.Receive(ref ipRec);
string recString = Encoding.UTF8.GetString(recBuffer);
if (recString.Equals("authent|password"))
{
//Send Passwort
Console.WriteLine("Password Required");
Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
{
PasswordWindow pw = new PasswordWindow();
pw.ShowDialog();
if (pw.ShowDialog() == true)
{
//send PW
}
else
{
//Dont send PW
}
}));
client.Send(bytes, bytes.Length, ip);
.
.
.
}
在PasswordWindow构造器崩溃。我试过STA + Dispatcher,MTA + Dispatcher,STA only ..我试过的任何东西都没有工作......我真的不知道它。
有人能解释我为什么仍然说线程需要是一个STA线程?
感谢您的任何帮助!
永远不会发现那个错误..即使我的编程老师还没有找到它.. 非常感谢:-) – Maaaario
OMG你有一个编程老师,他教你WPF?幸运的你!! –
是的,我们有:-) 他还教我们一些C++的游戏编程,但主要是我们正在做wpf :) – Maaaario