在启动画面中,我现在只使用演示计时器。 但我想使用一个使用win32类的类,并等待所有循环完成。虽然它循环使progressBar移动到100%。
在form1中,我还没有做任何事情。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
在Program.cs中我更改了Run:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Splash_Screen());
}
}
}
然后我为启动画面添加了一个新表单:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestingHardware
{
public partial class Splash_Screen : Form
{
Timer tmr;
public Splash_Screen()
{
InitializeComponent();
}
private void Splash_Screen_Load(object sender, EventArgs e)
{
}
private void Splash_Screen_Shown(object sender, EventArgs e)
{
tmr = new Timer();
//set time interval 3 sec
tmr.Interval = 3000;
//starts the timer
tmr.Start();
tmr.Tick += tmr_Tick;
}
void tmr_Tick(object sender, EventArgs e)
{
//after 3 sec stop the timer
tmr.Stop();
//display mainform
Form1 mf = new Form1();
mf.Show();
//hide this form
this.Hide();
}
}
}
在初始屏幕表单设计器中,我添加了progressBar。
最后获取硬件信息的表单。我希望在它循环结束时显示启动画面和progressBar。
using System;
using System.Collections;
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GetHardwareInfo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
cmbxOption.SelectedItem = "Win32_Processor";
}
private void InsertInfo(string Key, ref ListView lst, bool DontInsertNull)
{
lst.Items.Clear();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);
try
{
foreach (ManagementObject share in searcher.Get())
{
ListViewGroup grp;
try
{
grp = lst.Groups.Add(share["Name"].ToString(), share["Name"].ToString());
}
catch
{
grp = lst.Groups.Add(share.ToString(), share.ToString());
}
if (share.Properties.Count <= 0)
{
MessageBox.Show("No Information Available", "No Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (PropertyData PC in share.Properties)
{
ListViewItem item = new ListViewItem(grp);
if (lst.Items.Count % 2 != 0)
item.BackColor = Color.White;
else
item.BackColor = Color.WhiteSmoke;
item.Text = PC.Name;
if (PC.Value != null && PC.Value.ToString() != "")
{
switch (PC.Value.GetType().ToString())
{
case "System.String[]":
string[] str = (string[])PC.Value;
string str2 = "";
foreach (string st in str)
str2 += st + " ";
item.SubItems.Add(str2);
break;
case "System.UInt16[]":
ushort[] shortData = (ushort[])PC.Value;
string tstr2 = "";
foreach (ushort st in shortData)
tstr2 += st.ToString() + " ";
item.SubItems.Add(tstr2);
break;
default:
item.SubItems.Add(PC.Value.ToString());
break;
}
}
else
{
if (!DontInsertNull)
item.SubItems.Add("No Information available");
else
continue;
}
lst.Items.Add(item);
}
}
}
catch (Exception exp)
{
MessageBox.Show("can't get data because of the followeing error \n" + exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void RemoveNullValue(ref ListView lst)
{
foreach (ListViewItem item in lst.Items)
if (item.SubItems[1].Text == "No Information available")
item.Remove();
}
#region Control events ...
private void cmbxNetwork_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, chkNetwork.Checked);
}
private void cmbxSystemInfo_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, chkSystemInfo.Checked);
}
private void cmbxUtility_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, chkUtility.Checked);
}
private void cmbxUserAccount_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, chkUserAccount.Checked);
}
private void cmbxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
}
private void cmbxDeveloper_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, chkDeveloper.Checked);
}
private void cmbxMemory_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstMemory, chkMemory.Checked);
}
private void chkHardware_CheckedChanged(object sender, EventArgs e)
{
if (chkHardware.Checked)
RemoveNullValue(ref lstDisplayHardware);
else
InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
}
private void cmbxOption_SelectedIndexChanged(object sender, EventArgs e)
{
InsertInfo(cmbxOption.SelectedItem.ToString(), ref lstDisplayHardware, chkHardware.Checked);
}
private void chkDataStorage_CheckedChanged(object sender, EventArgs e)
{
if (chkDataStorage.Checked)
RemoveNullValue(ref lstStorage);
else
InsertInfo(cmbxStorage.SelectedItem.ToString(), ref lstStorage, chkDataStorage.Checked);
}
private void chkMemory_CheckedChanged(object sender, EventArgs e)
{
if (chkMemory.Checked)
RemoveNullValue(ref lstMemory);
else
InsertInfo(cmbxMemory.SelectedItem.ToString(), ref lstStorage, false);
}
private void chkSystemInfo_CheckedChanged(object sender, EventArgs e)
{
if (chkSystemInfo.Checked)
RemoveNullValue(ref lstSystemInfo);
else
InsertInfo(cmbxSystemInfo.SelectedItem.ToString(), ref lstSystemInfo, false);
}
private void chkNetwork_CheckedChanged(object sender, EventArgs e)
{
if (chkNetwork.Checked)
RemoveNullValue(ref lstNetwork);
else
InsertInfo(cmbxNetwork.SelectedItem.ToString(), ref lstNetwork, false);
}
private void chkUserAccount_CheckedChanged(object sender, EventArgs e)
{
if (chkUserAccount.Checked)
RemoveNullValue(ref lstUserAccount);
else
InsertInfo(cmbxUserAccount.SelectedItem.ToString(), ref lstUserAccount, false);
}
private void chkDeveloper_CheckedChanged(object sender, EventArgs e)
{
if (chkDeveloper.Checked)
RemoveNullValue(ref lstDeveloper);
else
InsertInfo(cmbxDeveloper.SelectedItem.ToString(), ref lstDeveloper, false);
}
private void chkUtility_CheckedChanged(object sender, EventArgs e)
{
if (chkUtility.Checked)
RemoveNullValue(ref lstUtility);
else
InsertInfo(cmbxUtility.SelectedItem.ToString(), ref lstUtility, false);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linkLabel1.LinkVisited = true;
System.Diagnostics.Process.Start("http://www.ShiraziOnline.net");
}
#endregion
}
}
它使用win32类,而是将其添加到ListView我想将每个部分添加到List。然后,当它完成所有循环并将列表添加到控件时,我想使用form1中的列表。或者我可能已经在form1加载它? 不确定。
答案 0 :(得分:0)
如果搜索者具有Count属性,请使用该属性作为最大值。使用以下方法确定当前百分比:
int count = 0;
try
{
foreach (ManagementObject share in searcher.Get())
{
count++;
progressbar1.Value = count * 100 / Math.Max(1, searcher.Count);
此外,请确保在添加项目之前使用.SuspendLayout()设置ListView以暂停更新,之后继续更新.ResumeLayout()因为不断更新ListView非常慢。 另一个建议是将所有ListViewItem放入List / Array中,然后只更新一次ListView。
在Clear()
之后的开头创建一个List lst.SuspendLayout();
lst.Items.Clear()
System.Collections.Generic.List<ListViewItem> listViewItems = new System.Collections.Generic.List<ListViewItem>();
然后,在将项目添加到列表&lt;&gt;
之前添加项目 listViewItems.Add(item);
最后,在处理完所有记录后,添加List&lt;&gt;到您的ListView并恢复更新
try
{
// All your code here
lst.Items.AddRange(listViewItems.ToArray());
}
finally
{
lst.ResumeLayout();
}