Files
RhythmicWallpaper/AccompanyingAssistant/WindowManager.cs

52 lines
1.5 KiB
C#

using AccompanyingAssistant;
public class WindowManager {
private MainForm? mainForm = null;
private Thread? uiThread = null;
public void ShowMain() {
if (mainForm == null || mainForm.IsDisposed) {
uiThread = new Thread(() => {
ApplicationConfiguration.Initialize();
mainForm = new MainForm();
Application.Run(mainForm);
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
// 等待窗体创建完毕(简单阻塞)
while (mainForm == null || !mainForm.IsHandleCreated) {
Thread.Sleep(10);
}
} else {
mainForm.Invoke(() => mainForm.Show());
}
}
public void ShowBubble(string message) {
ShowMain();
// 此处保证 ShowMessage 在 UI 线程上执行
if (mainForm != null && mainForm.IsHandleCreated) {
mainForm.Invoke(() => mainForm.ShowMessage(message));
}
}
public void CloseBubble() {
if (mainForm != null && mainForm.IsHandleCreated) {
mainForm.Invoke(() => mainForm.CloseBubble());
}
}
public void CloseMain() {
if (mainForm != null && mainForm.IsHandleCreated) {
mainForm.Invoke(() => {
mainForm.Close();
mainForm.Dispose();
mainForm = null;
});
}
}
}