我想在线程中打开一个窗口。正如您在我的代码中看到的那样,我想使用 Thread 类:
namespace WFPThreadin {
public partial class MainWindow : Window
{
private static Window1 win1;
private static Window2 win2;
Thread tid1, tid2;
public MainWindow()
{
InitializeComponent();
}
private void gomb_Click(object sender, RoutedEventArgs e)
{
if (((Button)sender).Name == "button1")
{
tid1 = new Thread(new ThreadStart(MainWindow.winshow1));
tid1.SetApartmentState(ApartmentState.STA);
tid1.Start();
}
if (((Button)sender).Name == "button2")
{
tid2 = new Thread(new ThreadStart(MainWindow.winshow2));
tid2.SetApartmentState(ApartmentState.STA);
tid2.Start();
}
}
public static void winshow1()
{
win1 = new Window1();
win1.Show();
}
public static void winshow2()
{
win2 = new Window2();
win2.Show();
}
}}
它效果不佳,因为当我单击按钮 1(或按钮 2)时,窗口 1(或窗口 2)会显示片刻......如果您有任何解决我的问题的建议,我将不胜感激!