I have the following situation: A WPF app with 2 pages (using navigation). On one of the pages there is a textbox which displays some system info using WMI queries and registry data.
The problem with this approach is that that info is always refreshed when I navigate to that page, which also means that all the tasks at work behind are re-done, which makes the texbox take some time before it starts displaying the info(yeah, some WMI queries are time consuming).
Since 99 percent of that data I need doesn't need refreshing(doesn't change), how would I go about performing it only once at and then bind it to that textbox so it doesn't always refresh?
textboxt code in home.xaml.cs (the page)
private void TextBox1_Loaded(object sender, RoutedEventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(delegate (object o, DoWorkEventArgs args)
{
});
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate (object o, RunWorkerCompletedEventArgs args)
{
TextBox1.Text = Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "ProcessorNameString", null).ToString().Replace("(R)", "").Replace("(TM)", "") + Environment.NewLine;
TextBox1.Text += "Memory: " + getRAMsize() + Environment.NewLine;
TextBox1.Text += "Free Space: " + GetTotalFreeSpace(sysdrive) + " GB" + Environment.NewLine;
if (Is64BitSystem)
{
TextBox1.Text += getOS() + " 64bit" + Environment.NewLine;
}
else
{
TextBox1.Text += getOS() + " 32bit" + Environment.NewLine;
}
TextBox1.Text += diskname() + Environment.NewLine;
TextBox1.Text += "MAC Address : " + System.Text.RegularExpressions.Regex.Replace(GetMacAddress().ToString(), ".{2}", "$&-").TrimEnd('-') + Environment.NewLine;
TextBox1.Text += av();
});
bw.RunWorkerAsync();
}
what it does vs what I want: Every time I navigate to it the textbox refreshes(textbox_loaded...of course) and it takes a second or two. I want to disable refreshing. I want it load once and keep those values until the app is opened again.
Wow...after much trial and error, I can't believe it was as easy as putting the code inside the TextBox1_Initialized event.
private void TextBox1_Initialized(object sender, RoutedEventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(delegate (object o, DoWorkEventArgs args)
{
});
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate (object o, RunWorkerCompletedEventArgs args)
{
TextBox1.Text = Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "ProcessorNameString", null).ToString().Replace("(R)", "").Replace("(TM)", "") + Environment.NewLine;
TextBox1.Text += "Memory: " + getRAMsize() + Environment.NewLine;
TextBox1.Text += "Free Space: " + GetTotalFreeSpace(sysdrive) + " GB" + Environment.NewLine;
if (Is64BitSystem)
{
TextBox1.Text += getOS() + " 64bit" + Environment.NewLine;
}
else
{
TextBox1.Text += getOS() + " 32bit" + Environment.NewLine;
}
TextBox1.Text += diskname() + Environment.NewLine;
TextBox1.Text += "MAC Address : " + System.Text.RegularExpressions.Regex.Replace(GetMacAddress().ToString(), ".{2}", "$&-").TrimEnd('-') + Environment.NewLine;
TextBox1.Text += av();
});
bw.RunWorkerAsync();
}