当我启动它时,我的form1是不可见的。 (背景线程) [英] My form1 is invisible when i start it. (background threading)

查看:61
本文介绍了当我启动它时,我的form1是不可见的。 (背景线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动它时,我当前的Form1是不可见的。



当我启动我的应用程序时,

我希望我的Form1可见

然后,label4显示公告

然后,一个StartPage()方法需要5秒才能加载。

然后,label4到dissapear。



我试过Application.DoEvents();并没有效果。

然后我尝试使用Application.Run();但它没有做我想要的。



我该怎么办?请使用吗?

My current Form1 is invisible when I start it.

When I start my application,
I want my Form1 being visible
then, label4 with the announcement being displayed
then, a StartPage() method that takes 5seconds to load.
and then, the label4 to dissapear.

I tried with Application.DoEvents(); and no effect.
Then I tried with Application.Run();but it is not doing what I want.

What should I use, please?

private void Form1_Load(object sender, EventArgs e)
{
    Application.DoEvents();
  //  Application.Run(); //this just skip everything
    label4.Visible = true;
    StartPage();//is taking 5s to load
    label4.Visible = false;
}



谢谢。


Thanks.

推荐答案

如果可能,你应该删除来自加载事件处理程序的耗时代码。您可以将它放在 BackgroundWorker [ ^ ]。
If possible, you should remove the time-consuming code from the Load event handler. You might put it in a BackgroundWorker[^].


首先,正如Carlo所说,你应该将耗时的工作转移到后台线程中,以释放你的UI线程。永远不要使用Application.DoEvents - 如果必须的话,这是一个好的迹象,表明你正在以完全错误的方式做某事。



我会从它开始Form.Shown事件也是负载:

First off, as Carlo has said, you should move time-consuming work into a background thread, to free up your UI thread. Never use Application.DoEvents - if you have to, it's a good sign that you are doing something in totally the wrong way.

And I'd start it from the Form.Shown event as well rather than the Load:
private void Form1_Shown(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += work_DoWork;
    work.RunWorkerCompleted += work_RunWorkerCompleted;
    label4.Visible = true;
    work.RunWorkerAsync();
    }

void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    label4.Visible = false;
    }

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    StartPage();
    }


这篇关于当我启动它时,我的form1是不可见的。 (背景线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆