Hi
I have written a program that gets data from hardware over the serial port. The data varies from 15 seconds to 5 hours worth.
I work out how many milliseconds the thread.sleep needs to be with a while loop and update a progress bar.
The problem is that on some computers it works fine, but others it's so slow that the system hangs.
Is there a betterway to do this handling? I think that older and slower cpu's are the problem ans that the thread.sleep does not count in realtime any more.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
ClearData();
try
{
startValues1 = serialPort1.ReadTo("");
String[] subData1 = startValues1.Split(new char[] { ' ' });
double recordedTime = Convert.ToDouble(subData1[13]);
int dataTime1 = Convert.ToInt32((recordedTime * 0.1) * 60);
int dataTime = (dataTime1 * 10) + 2000;
int n = 0;
int value1 = 0;
//Set the total number of steps to 100 (or 100%)
progressBar1.Maximum = dataTime;
progressBar1.Minimum = 0;
//Set the initial value of the progress bar to 0% completed
progressBar1.Step = 1;
//If your progress bar is already visible you don't need this. But this is one of those objects I like to hide when I'm not using it
progressBar1.Visible = true;
//This next line tells your application to wait or go to sleep for 1000ms / 1 second
while (n <= dataTime)
{
System.Threading.Thread.Sleep(1);
n = n + 1;
progressBar1.Value = value1;
value1 = value1 + 1;
}
//System.Windows.Forms.MessageBox.Show("Download Complete!");
PortData += serialPort1.ReadExisting();