#region Driver Data Display
public class DriverDisplay
{
private WebClient _wc = new WebClient();
ProgressBar _Progress;
Label _Label;
TextBox _tBox;
Form _Frm;
public DriverDisplay()
{
_wc.DownloadProgressChanged += _wc_DownloadProgressChanged;
_wc.DownloadStringCompleted += _wc_DownloadStringCompleted;
Form frm = new Form();
frm.Size = new Size(500, 375);
frm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
frm.Text = "Downloading Data...";
ProgressBar p = new ProgressBar();
p.Size = new Size(450, 20);
p.Location = new Point(18, 15);
p.Maximum = 100;
p.Value = 0;
p.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
_Progress = p;
Label lbl = new Label();
lbl.Text = "Progress: " + p.Value.ToString();
lbl.Width = frm.Width - 12;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Location = new Point(0, 35);
lbl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
_Label = lbl;
TextBox T = new TextBox();
T.Multiline = true;
T.ScrollBars = ScrollBars.Both;
T.WordWrap = false;
T.Size = new Size(450, 240);
T.Location = new Point(18, 75);
T.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
_tBox = T;
frm.Controls.Add(p);
frm.Controls.Add(lbl);
frm.Controls.Add(T);
frm.FormClosing += _Frm_FormClosing;
_Frm = frm;
}
private void UpdateValues(int progress)
{
try
{
_Frm.Invoke((MethodInvoker)delegate
{
int i = progress + (100 * (totalLinks - downloadQueue.Count));
_Progress.Value = i;
_Progress.Update();
string s = ((double)i / _Progress.Maximum * 100).ToString();
_Label.Text = "Progress: " + s.Substring(0, s.IndexOf('.') > -1 ? s.IndexOf('.') + 3 : s.Length) + "%";
_Label.Update();
});
}
catch (ObjectDisposedException) { }
}
int num;
int totalBytes;
int totalLinks;
Queue<MethodInvoker> downloadQueue;
public void GetDriverData(string[] drivers)
{
_Frm.Show();
num = 0;
totalBytes = 0;
totalLinks = drivers.Length;
downloadQueue = new Queue<MethodInvoker>(drivers.Length);
_Progress.Maximum = drivers.Length * 100;
foreach (string d in drivers)
downloadQueue.Enqueue((MethodInvoker)delegate { _wc.DownloadStringAsync(new Uri("https://www.sysnative.com/drivers/driver.php?id=" + d), d); });
downloadQueue.ElementAt(0).Invoke();
}
private void _wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
UpdateValues(e.ProgressPercentage);
}
private void _wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (!e.Cancelled || e.Error != null)
{
string html = e.Result;
string driver = e.UserState.ToString();
RegexOptions RO = RegexOptions.IgnoreCase | RegexOptions.Singleline;
string desc = Regex.Match(html, @"Driver Description:\s+</strong>.*?\b(.*?)\b</p>", RO).Groups[1].Value;
string updateSite = Regex.Match(html, @"<strong>Driver Update Site:\s+</strong>((.*?<a href="".*?"">(?<update>.*?)</a>)|(<span class=""red"">(?<update>.*?)</span>))", RO).Groups["update"].Value;
_tBox.AppendText(string.Format("Driver: {0}\r\nDescription: {1}\r\nUpdate: {2}\r\n\r\n", driver, desc, updateSite));
}
else
{
MessageBox.Show(e.Error.Message);
}
num++;
totalBytes += e.Result.Length;
downloadQueue.Dequeue();
if (downloadQueue.Count > 0)
downloadQueue.ElementAt(0).Invoke();
else
_Frm.Text = string.Format("[{0} Drivers] Async Downloaded: {1} bytes", num, totalBytes);
}
private void _Frm_FormClosing(object sender, FormClosingEventArgs e)
{
if (downloadQueue.Count > 0)
{
e.Cancel = true;
MessageBox.Show("Please wait until the driver data has been downloaded from the table...", "Download in progress", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
#endregion