Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I click on the button the code runs correctly but the text box does not update in the UI until the entire while loop has finished. I need it to update the text box on every loop.

C#
private void btnBeginTransfer_Click(object sender, RoutedEventArgs e)
       {

           serial.PortName = port;
           serial.BaudRate = baud;
           serial.DataBits = dataBit;
           serial.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBit);
           serial.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake);
           serial.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
           serial.ReadTimeout = rTimeout;
           serial.WriteTimeout = wTimeout;
           serial.Open();

           //reading file and writing it across
           using (System.IO.TextReader reader = System.IO.File.OpenText(txtSelect.Text))
           {
               string line;

               while ((line = reader.ReadLine()) != null)
               {
                   serial.WriteLine(line);
                   //updating the text box
                   txtTransferredData.Text += line + "\n";

               }
           }

           serial.Close();

       }


Sample code would be very helpful.
Posted
Comments
Christian Amado 9-Oct-14 10:53am    
Using BackgroundWorker.
ZurdoDev 9-Oct-14 10:53am    
You'll have to use multiple threads I believe because the default is the UI is on that same thread and so won't update until it is done.

Hi,

You can use background worker so your UI wont be stop till the time your while loop is running. Let me know if it wont work for you.

Refer : Background Worker[^]

Thanks
 
Share this answer
 
Comments
Bryan Fil 9-Oct-14 11:08am    
I have tried to write in a background worker and cant seem to get it working right. All the example i see have if loops and throw me off.
ZurdoDev 9-Oct-14 11:23am    
Then I suggest you start a new question and post the relevant code and show us what is happening.
You will required to have the Refresh applied on your textbox so it will display all the texts while the loop is running.

You will require to have a static class for ExtensionMethods that will contain your refresh routine.

C#
public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    }



now simply calling
txtTransferredData.Refresh()
will refresh your textbox text within loop as well.
I think this will work in your case.
 
Share this answer
 
Comments
Bryan Fil 16-Oct-14 9:55am    
I have inserted the Extension Method but when I use txtTransferredData.Refresh(); I get an error saying
'System.Windows.Controls.TextBox' does not contain a definition for 'Refresh' and no extension method 'Refresh' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?
Bryan Fil 16-Oct-14 10:30am    
Got it working, I put the Extension method in the wrong place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900