Note that there are some explanatory texts on larger screens.

plurals
  1. POProgressBar not updating
    primarykey
    data
    text
    <p>I have a WPF form containing a button and a progress bar element. When I click the button it calls a function from another class that begins a background worker which is responsible for reading/writing data through a serial port to a device I am programming a windows interface for.</p> <p>Previously I was using a new thread, but I decided to try a background worker, but still no luck.</p> <p>What is happening is I click the button and the progress bar stays at 0% until my time consuming (10+ seconds) for loop is finished, and then the progress jumps to 100%</p> <p>Any help is appreciated, thanks in advance. Below is the code for my interface logic:</p> <pre><code>private void connectButton_Click(object sender, RoutedEventArgs e) //Connect button clicked { if (!Global.isConnected) { device.readValues((string)portsOpen.SelectedValue, connectProgress); portsOpen.IsEnabled = false; connectButton.Content = "Disconnect"; } else { device.disconnect(); portsOpen.IsEnabled = true; connectButton.Content = "Connect"; } } </code></pre> <p>And below is the code for my additional class:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Controls; using System.ComponentModel; namespace lookoutSecurity { class agentRadioRead { /*Local Variable Declarations*/ SerialPort sp; BackgroundWorker worker; int attempt = 0; public string[] populateAvailablePorts() //Returns string array of open ports { String[] portsAvailable = SerialPort.GetPortNames(); return portsAvailable; } public void readValues(string selectedPort, ProgressBar progressBar) //Function that sets up serial port and background worker { sp = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); worker.RunWorkerAsync(progressBar); } private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) //Update user interface with current percentage { ProgressBar progressBar = e.UserState as ProgressBar; App.Current.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Send, new Action( delegate() { progressBar.Value = e.ProgressPercentage; })); } private void worker_DoWork(object sender, DoWorkEventArgs e) //Initial starting point for reading data { startRead((ProgressBar)e.Argument); } private string sendInitial() //Sends ? and returns init ASUTTON { string init = ""; sp.Write("?"); for (int i = 0; i &lt; 7; i++) { try { init += sp.ReadByte().ToString(); } catch { break; } } return init; } private void startRead(ProgressBar progressBar) //Initializes data send and receive { sp.ReadTimeout = 5000; sp.Open(); string init = sendInitial(); if (init == Global.AGENT_RADIO) { sp.Write(Global.READ_VALUES, 0, Global.READ_VALUES.Length); byte key = (byte)sp.ReadByte(); if (Global.START_COMMAND == key) { for (int i = 1; i &lt; 2247; i++) { object progressBarObject = new object(); progressBarObject = progressBar; int progress = (i / 2246) * 100; worker.ReportProgress(progress, progressBarObject); readData(i, progressBar); sp.Write(Global.GO_AHEAD, 0, Global.GO_AHEAD.Length); sp.DiscardInBuffer(); sp.DiscardOutBuffer(); } } else { System.Windows.MessageBox.Show("Connection Failed - Invalid Key: " + key); sp.Close(); } } else { sp.Close(); if (attempt &lt; 3) { attempt++; startRead(progressBar); } else { System.Windows.MessageBox.Show("Connection Failed - Not Agent Radio: " + init); } } } private void readData(int iteration, ProgressBar progress) //Reads data for current iteration { byte value = 0; try { value = (byte)sp.ReadByte(); } catch { sp.Close(); Global.isConnected = false; if (attempt &lt; 3) { attempt++; startRead(progress); } else { System.Windows.MessageBox.Show("Read Timeout - Value Number: " + iteration); App.Current.Shutdown(); } } if (iteration &gt; 0 &amp;&amp; iteration &lt; 385) //read schedule { double pos = (iteration - 1) / 48; int i = (int)Math.Floor(pos); int j = (iteration - 1) - (i * 48); Live.schedule[i, j] = value; } if (iteration &gt; 384 &amp;&amp; iteration &lt; 1285) //read alarm history { double pos = (iteration - 385) / 9; int i = (int)Math.Floor(pos); int j = (iteration - 385) - (i * 9); Live.alarms[i, j] = value; } if (iteration &gt; 1284 &amp;&amp; iteration &lt; 1345) //read error log { double pos = (iteration - 1285) / 6; int i = (int)Math.Floor(pos); int j = (iteration - 1285) - (i * 6); Live.errors[i, j] = value; } if (iteration &gt; 1344 &amp;&amp; iteration &lt; 1945) //read voltage history { double pos = (iteration - 1345) / 6; int i = (int)Math.Floor(pos); int j = (iteration - 1345) - (i * 6); Live.voltage[i, j] = value; } if (iteration &gt; 1944 &amp;&amp; iteration &lt; 1973) //read holidays { Live.holidays[iteration - 1945] = value; } if (iteration &gt; 1972 &amp;&amp; iteration &lt; 2168) //read message sequences { double pos = (iteration - 1973) / 15; int i = (int)Math.Floor(pos); int j = (iteration - 1973) - (i * 15); Live.messages[i, j] = value; } if (iteration &gt; 2167 &amp;&amp; iteration &lt; 2196) //read message info { Live.recordings[iteration - 2168] = value; } if (iteration &gt; 2195 &amp;&amp; iteration &lt; 2246) //read sysval { Live.sysval[iteration - 2196] = value; } if (iteration == 2246 &amp;&amp; value == Global.FINISH_COMMAND) { System.Windows.MessageBox.Show("All values from the device have been received.", "Data Received", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information); Global.isConnected = true; } } public void disconnect() //Disconnects device { if (Global.isConnected == true) { sp.Write(Global.EXIT_PROGRAM, 0, Global.EXIT_PROGRAM.Length); Global.isConnected = false; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload