Andrew

Andrew

  • NA
  • 3
  • 0

Updating User Interface

Jul 10 2009 3:04 AM


Hi, and welcome to my first post on any forum!
 

I'm a LabVIEW developer by trade, and have begun an attempt to learn C# as I have heard many praises.

 
My biggest problem at the moment, is being able to update the UI while the program is working.

All my App does, is ping a list of PC's and return the time or it's success. Unfortunately, the example that I have written, does not do this. The UI is kept stagnant until it has completed all it's tasks. WHY!?
This HAS to be a noob mistake, but for the life of me I can't see where it is coming from. Do I have to create a seperate UI thread!? Surely this is too advanced. What about the BackgroundWorker stuff?
 
using
System;
using
System.IO;
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.Net.NetworkInformation;
namespace
MESNetworkAnalyser
{
public partial class MainForm : Form
{
private string[] computerList;
BackgroundWorker bw = new BackgroundWorker();
public MainForm()
{
InitializeComponent();
bw.WorkerSupportsCancellation =
true;
bw.WorkerReportsProgress =
true;
}
private void MainForm_Load(object sender, EventArgs e)
{
computerList = Read_Computer_List(
"computerList.txt");

int inc = 0;
foreach (string computer in computerList)
{
computerList.SetValue(computer.Trim(),inc);
inc++;
}
ProgressText.Lines = computerList;
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;

}
private string[] Read_Computer_List(string fileName)
{
StreamReader sr = new StreamReader(fileName);
string fileContents = sr.ReadToEnd();
string[] fileLines = fileContents.Split(new char[] {'\n'});
return fileLines;
}
private string ping(string computerName)
{
computerName.Trim();
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment =
true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
try
{
PingReply reply = pingSender.Send(computerName, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
return string.Concat("RoundTrip time: ", reply.RoundtripTime);
}
else
{
return "No Connection";
}
}
catch
{
return "No Connection";
}
}
private void StartButton_Click(object sender, EventArgs e)
{
int inc = 0;
int listSize = computerList.Length;
int successCounter = 0;
foreach (string computer in computerList)
{
string pingReply = ping(computer);
string[] tempArray = ProgressText.Lines;
tempArray[inc] =
String.Concat(computer, "...", pingReply);
ProgressText.Lines = tempArray;
if (!pingReply.Equals("No Connection"))
{
successCounter++;
}
inc++;
totalProgressBar.Value = inc / listSize * 100;
Thread.Sleep(1000);
}
ResultsText.Text =
String.Concat(successCounter, "/", listSize, " computers were found on the network");
}
}
}

 

Answers (3)