【问题标题】:C# Application ThreadingC# 应用程序线程
【发布时间】:2016-02-27 05:29:36
【问题描述】:

所以我正在创建一个应用程序,该应用程序将具有一个加载资源的启动屏幕,完成后将加载应用程序的主窗体。在过去,我只是让启动屏幕拥有线程并保持隐藏状态,但我正在玩弄实际上让我的主窗体成为线程所有者的想法,但我在窗体之间转换时遇到了麻烦。我目前的做法是

void main(String[] args)
{
   ApplicationContext appContext = new ApplicationContext(new SplashScreen());
   appContext.ThreadExit += appContext_ThreadExit;
   Application.Run(appContext);
}
place
private void appContext_ThreadExit(object sender, EventArgs e)
{
   Application.Run(new MainForm());
}

这给了我一个错误,即您无法在线程上启动新的消息循环。那么我该如何正确执行这个过渡呢?还是我已经通过允许 SplashScreen 拥有线程来使用最佳方法?

【问题讨论】:

  • 为什么不使用异步?让启动画面使用 async/await 加载资源
  • 更多代码不会有帮助吗?你只是显示有限的代码集

标签: c# winforms


【解决方案1】:

这是我想出的,但如果有人有更优雅或更合适的方法,请随时告诉我。

bool isLoading = true;

MainForm.OnLoad()
   -> creates and runs SplashScreen

MainForm.OnShow()
   -> if isLoading is true, re-hide

SplashScreen.LoadingComplete
   -> event to signal completion of loading events

MainForm.SplashScreenLoadingComplete
   -> handler sets isLoading to false
   -> calls Show() this time form will show

我使用后台工作人员处理 SplashScreen 中的加载

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2010-10-12
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多