Cross-thread cross-form. Display a splash screen with a progress bar(跨螺纹交叉形式。显示带有进度条的初始屏幕)
问题描述
我的解决方案:
所以我设法找到了另一个教程http://www.codeproject.com/KB/dotnet/Yet_Another_Splash_Screen.aspx,源代码对我来说似乎更有意义。以下是我现在使用的代码。Main()
保持不变。
Splash.cs
`
公共分部类FRM_Splash:Form { 委托无效进度委托(int百分比); 委托void SplashShowCloseDelegate();/// <summary> /// To ensure splash screen is closed using the API and not by keyboard or any other things /// </summary> bool CloseSplashScreenFlag = false; /// <summary> /// Base constructor /// </summary> /// public Frm_Splash() { InitializeComponent(); progress_Splash.Show(); this.ClientSize = this.BackgroundImage.Size; } public void ShowSplashScreen() { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new SplashShowCloseDelegate(ShowSplashScreen)); return; } this.Show(); Application.Run(this); } /// <summary> /// Closes the SplashScreen /// </summary> public void CloseSplashScreen() { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new SplashShowCloseDelegate(CloseSplashScreen)); return; } CloseSplashScreenFlag = true; this.Close(); } /// <summary> /// Update text in default green color of success message /// </summary> /// <param name="Text">Message</param> public void Progress(int percent) { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new ProgressDelegate(Progress), new object[] { percent }); return; } // Must be on the UI thread if we've got this far progress_Splash.Value = percent; // Fade in the splash screen - looks pro. :D if (percent < 10) this.Opacity = this.Opacity + .15; } /// <summary> /// Prevents the closing of form other than by calling the CloseSplashScreen function /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SplashForm_FormClosing(object sender, FormClosingEventArgs e) { if (CloseSplashScreenFlag == false) e.Cancel = true; } }`
Form1.cs
public partial class Frm_Main : Form { Frm_Splash frm_Splash = new Frm_Splash(); public Frm_Main() { this.Hide(); Thread splashthread = new Thread(new ThreadStart(frm_Splash.ShowSplashScreen)); splashthread.IsBackground = true; splashthread.Start(); InitializeComponent(); CenterToScreen(); } private void Frm_Main_Load(object sender, EventArgs e) { if (PassedAll() == true) FillMovieLB(); if (FillMovieProgress == 100) { //Throw in this sleep so the user can see the progress bar reach all the way to the end. Thread.Sleep(1000); this.Show(); frm_Splash.CloseSplashScreen(); this.Activate(); } }
原始问题
G‘day all,
我对C#编程非常陌生,我对http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx教程和在我的应用程序中实现它有问题。我发现有点难以理解问题出在哪里。我知道让这个闪屏工作有很多事情,但我就是想不通。
当我启动程序时,会显示Frm_Main
,您可以看到listbox
正在填充,因为我把它放在BackgroundWorker.DoWork()
中,然后工作完成后会显示我的frm_Splash
。显然,它的工作方式是,在Frm_Main
上的工作过程中会显示frm_Splash
,进度条会显示加载的进度(这部分我还没有实现)。
编辑:我可能说不清楚,但问题是:如何在工作完成期间和主窗体显示之前显示闪屏?
谢谢大家。:)
以下是我的代码:
static Frm_Splash frm_Splash = new Frm_Splash();
public delegate void ShowFormDelegate();
public void ShowForm()
{
frm_Splash.Show();
}
public Frm_Main()
{
InitializeComponent();
CenterToScreen();
if (PassedAll() == true)
{
back_loadprog.RunWorkerAsync();
}
}
private void back_loadprog_DoWork(object sender, DoWorkEventArgs e)
{
Invoke(new ShowFormDelegate(ShowForm));
Invoke(new FillMovieLBDelegate(FillMovieLB));
}
推荐答案
这里,有一些代码...适合我。
开机画面:
namespace Screens.Forms
{
public partial class Splash : DevExpress.XtraEditors.XtraForm
{
public Splash()
{
InitializeComponent();
}
string RandomLoadingMessage()
{
string[] lines ={
"Pripremam warp pogon",
"Moj drugi ekran za učitavanje je brži, probaj njega",
"Verzija programa koju imam u testiranju imala je smiješnije poruke"
};
return lines[new Random().Next(lines.Length)];
}
public void RandomizeText()
{
lblMessage.Text = RandomLoadingMessage();
}
private void Splash_Load(object sender, EventArgs e)
{
RandomizeText();
}
private static Splash _splash;
private static bool _shouldClose;
static void ThreadFunc()
{
_splash = new Splash();
_splash.Show();
while (!_shouldClose)
{
Application.DoEvents();
Thread.Sleep(100);
if (new Random().Next(1000) < 10)
{
_splash.Invoke(new MethodInvoker(_splash.RandomizeText));
}
}
for (int n = 0; n < 18; n++)
{
Application.DoEvents();
Thread.Sleep(60);
}
if (_splash != null)
{
_splash.Close();
_splash = null;
}
}
static public void ShowSplash()
{
_shouldClose = false;
Thread t = new Thread(ThreadFunc);
t.Priority = ThreadPriority.Lowest;
t.Start();
}
internal static void RemoveSplash()
{
_shouldClose = true;
}
internal static void ShowSplash(List<string> fromTwitterMessages)
{
ShowSplash();
}
}
}
显示为:
Splash.ShowSplash();
做您需要的工作,然后完成:
Splash.RemoveSplash();
这篇关于跨螺纹交叉形式。显示带有进度条的初始屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:跨螺纹交叉形式。显示带有进度条的初始屏幕
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01