-1
我做了一個閃屏形式:如何計算百分比並更新splashscreen窗體中的progressBar?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace GetHardwareInfo
{
public partial class SplashScreen : Form
{
public SplashScreen()
{
InitializeComponent();
}
private Mutex mutex = new Mutex();
public void SyncedClose()
{
mutex.WaitOne();
this.Close();
mutex.ReleaseMutex();
}
public void UpdateProgressBar(int percentage)
{
if (this.InvokeRequired)
{
mutex.WaitOne();
if (!IsDisposed)
this.BeginInvoke(new Action<int>(UpdateProgresPRV), percentage);
mutex.ReleaseMutex();
}
else
{
UpdateProgresPRV(percentage);
}
}
private void UpdateProgresPRV(int per)
{
if (progressBar1.IsDisposed) return;
progressBar1.Value = per;
}
private void SplashScreen_Load(object sender, EventArgs e)
{
}
}
}
濺射屏幕的形式在設計一個背景圖像和圖像的progressBar1上。
然後在Form1我沒有在頂部:
List<string> WmiClassesKeys = new List<string>();
IEnumerable<Control> controls;
string comboBoxesNames;
SplashScreen splash = new SplashScreen();
然後在構造:
controls = LoopOverControls.GetAll(this, typeof(ComboBox));
string[] lines = File.ReadAllLines(@"c:\wmiclasses\wmiclasses1.txt");
foreach (string line in lines)
{
foreach (ComboBox comboBox in controls.OfType<ComboBox>())
{
if (line.StartsWith("ComboBox"))
{
comboBoxesNames = line.Substring(14);
}
else
{
if (line.StartsWith("Classes"))
{
if (comboBox.Name == comboBoxesNames)
{
comboBox.Items.Add(line.Substring(14));
}
}
}
}
}
foreach (ComboBox comboBox in controls.OfType<ComboBox>())
{
comboBox.SelectedIndex = 0;
}
方法GETALL是遍歷特定控件:
public static IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
當我正在運行該程序,這需要一些時間來使foreach循環,我需要顯示SplashScreen和約會progressBar直到foreach循環結束。
這是怎麼看起來像現在:
您正在發佈的方式太多的信息 - 坦率地說,我懷疑很多人會費心閱讀所有這些信息。我注意到的一件事是你有一個Thread.Sleep()調用。這絕不應該在WinForms程序的UI線程上使用。相反,您應該重寫程序的這一部分以使用WinForms Timer(而不是Thread Timer),並在Timer的Tick事件處理程序中完成需要完成的工作。 – RenniePet