Results 1 to 9 of 9

Thread: updating textbox with thread?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    115

    updating textbox with thread?

    I have loop that goes through a txt file that has about 1000000 lines of text.
    I have a var. x that changes every time the program reads a line of text
    Is it possible to update textbox with that x and actually see the update.
    I really don't want to use DoEvents but I think it is possible with threads
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating textbox with thread?

    How much time is passing between changes in this value? If it's very short then is it really practical to show the changes to the user anyway? If you set the Text of the TextBox and call its Refresh method each time then the TextBox will be redrawn.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating textbox with thread?

    Note that if you read the file in the main thread then your UI will be unusable for the time it is running. You can read the file in a different thread, certainly. You haven't specified which version you're using (use the radio buttons on the "new thread" page) so I can't tell you whether to use a BackgroundWorker or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    115

    Re: updating textbox with thread?

    that's the unfinished code for the form
    4 textboxes
    3 buttons

    VB Code:
    1. using System;
    2. using System.Drawing;
    3. using System.Collections;
    4. using System.ComponentModel;
    5. using System.Windows.Forms;
    6. using System.Data;
    7. using System.IO;
    8. using System.Threading;
    9.  
    10. namespace project
    11. {
    12.     /// <summary>
    13.     /// Summary description for Form1.
    14.     /// </summary>
    15.     public class Form1 : System.Windows.Forms.Form
    16.     {
    17.         private System.Windows.Forms.OpenFileDialog openFileDialog1;
    18.         private System.Windows.Forms.Button button1;
    19.         private System.Windows.Forms.Button button2;
    20.         private System.Windows.Forms.TextBox textBox1;
    21.         private System.Windows.Forms.TextBox textBox2;
    22.         private System.Windows.Forms.TextBox textBox3;
    23.         private System.Windows.Forms.TextBox textBox4;
    24.         /// <summary>
    25.         /// Required designer variable.
    26.         /// </summary>
    27.         private System.ComponentModel.Container components = null;
    28.         private System.Windows.Forms.Button button3;
    29.  
    30.         public Class1 myClass = new Class1();
    31.         public  int x=0;
    32.         private Thread trd;
    33.  
    34.         public Form1()
    35.         {
    36.             //
    37.             // Required for Windows Form Designer support
    38.             //
    39.             InitializeComponent();         
    40.  
    41.             //
    42.             // TODO: Add any constructor code after InitializeComponent call
    43.             //
    44.  
    45.         }
    46.  
    47.         /// <summary>
    48.         /// Clean up any resources being used.
    49.         /// </summary>
    50.         protected override void Dispose( bool disposing )
    51.         {
    52.             if( disposing )
    53.             {
    54.                 if (components != null)
    55.                 {
    56.                     components.Dispose();
    57.                 }
    58.             }
    59.             base.Dispose( disposing );
    60.         }
    61.  
    62.         #region Windows Form Designer generated code
    63.         /// <summary>
    64.         /// Required method for Designer support - do not modify
    65.         /// the contents of this method with the code editor.
    66.         /// </summary>
    67.         private void InitializeComponent()
    68.         {
    69.             this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
    70.             this.button1 = new System.Windows.Forms.Button();
    71.             this.button2 = new System.Windows.Forms.Button();
    72.             this.textBox1 = new System.Windows.Forms.TextBox();
    73.             this.textBox2 = new System.Windows.Forms.TextBox();
    74.             this.textBox3 = new System.Windows.Forms.TextBox();
    75.             this.textBox4 = new System.Windows.Forms.TextBox();
    76.             this.button3 = new System.Windows.Forms.Button();
    77.             this.SuspendLayout();
    78.             //
    79.             // button1
    80.             //
    81.             this.button1.Location = new System.Drawing.Point(312, 8);
    82.             this.button1.Name = "button1";
    83.             this.button1.Size = new System.Drawing.Size(88, 20);
    84.             this.button1.TabIndex = 0;
    85.             this.button1.Text = "select";
    86.             this.button1.Click += new System.EventHandler(this.button1_Click);
    87.             //
    88.             // button2
    89.             //
    90.             this.button2.Location = new System.Drawing.Point(312, 32);
    91.             this.button2.Name = "button2";
    92.             this.button2.Size = new System.Drawing.Size(88, 20);
    93.             this.button2.TabIndex = 1;
    94.             this.button2.Text = "select";
    95.             this.button2.Click += new System.EventHandler(this.button2_Click);
    96.             //
    97.             // textBox1
    98.             //
    99.             this.textBox1.Location = new System.Drawing.Point(8, 8);
    100.             this.textBox1.Name = "textBox1";
    101.             this.textBox1.Size = new System.Drawing.Size(285, 20);
    102.             this.textBox1.TabIndex = 2;
    103.             this.textBox1.Text = "";
    104.             //
    105.             // textBox2
    106.             //
    107.             this.textBox2.Location = new System.Drawing.Point(8, 32);
    108.             this.textBox2.Name = "textBox2";
    109.             this.textBox2.Size = new System.Drawing.Size(285, 20);
    110.             this.textBox2.TabIndex = 3;
    111.             this.textBox2.Text = "";
    112.             //
    113.             // textBox3
    114.             //
    115.             this.textBox3.Location = new System.Drawing.Point(416, 8);
    116.             this.textBox3.Name = "textBox3";
    117.             this.textBox3.Size = new System.Drawing.Size(80, 20);
    118.             this.textBox3.TabIndex = 4;
    119.             this.textBox3.Text = "";
    120.             this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
    121.             //
    122.             // textBox4
    123.             //
    124.             this.textBox4.Location = new System.Drawing.Point(416, 32);
    125.             this.textBox4.Name = "textBox4";
    126.             this.textBox4.Size = new System.Drawing.Size(80, 20);
    127.             this.textBox4.TabIndex = 5;
    128.             this.textBox4.Text = "";
    129.             //
    130.             // button3
    131.             //
    132.             this.button3.Location = new System.Drawing.Point(512, 32);
    133.             this.button3.Name = "button3";
    134.             this.button3.Size = new System.Drawing.Size(80, 20);
    135.             this.button3.TabIndex = 6;
    136.             this.button3.Text = "start";
    137.             this.button3.Click += new System.EventHandler(this.button3_Click);
    138.             //
    139.             // Form1
    140.             //
    141.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    142.             this.ClientSize = new System.Drawing.Size(696, 86);
    143.             this.Controls.Add(this.button3);
    144.             this.Controls.Add(this.textBox4);
    145.             this.Controls.Add(this.textBox3);
    146.             this.Controls.Add(this.textBox2);
    147.             this.Controls.Add(this.textBox1);
    148.             this.Controls.Add(this.button2);
    149.             this.Controls.Add(this.button1);
    150.             this.Name = "Form1";
    151.             this.Text = "Form1";
    152.             this.Load += new System.EventHandler(this.Form1_Load);
    153.             this.ResumeLayout(false);
    154.  
    155.         }
    156.         #endregion
    157.  
    158.         /// <summary>
    159.         /// The main entry point for the application.
    160.         /// </summary>
    161.         [STAThread]
    162.         static void Main()
    163.         {
    164.             Application.Run(new Form1());
    165.            
    166.         }
    167.  
    168.         private void button1_Click(object sender, System.EventArgs e)
    169.         {
    170.             openFileDialog1.ShowDialog();
    171.             myClass.setPath1(openFileDialog1.FileName);
    172.             textBox1.Text = myClass.getPath1();
    173.         }
    174.  
    175.         private void button2_Click(object sender, System.EventArgs e)
    176.         {
    177.             openFileDialog1.ShowDialog();
    178.             myClass.setPath2(openFileDialog1.FileName);
    179.             textBox2.Text = myClass.getPath2();
    180.         }
    181.  
    182.         private void Form1_Load(object sender, System.EventArgs e)
    183.         {
    184.             Thread trd = new Thread(new ThreadStart(this.ThreadTask));
    185.             trd.IsBackground = true;
    186.             trd.Start();
    187.         }
    188.  
    189.         private void textBox3_TextChanged(object sender, System.EventArgs e)
    190.         {
    191.        
    192.         }
    193.  
    194.         private void button3_Click(object sender, System.EventArgs e)
    195.         {
    196.             String [] myArr;
    197.             String line;
    198.             ArrayList myList = new ArrayList();
    199.  
    200.             using (StreamReader sr = new StreamReader(textBox1.Text))
    201.             {
    202.            
    203.                 while ((line = sr.ReadLine()) != null)
    204.                 {
    205.                     myList.Add(sr.ReadLine());
    206.                     x ++;                  
    207.                 }
    208.             //trd.Abort();
    209.             }
    210.         }
    211.         private void ThreadTask()
    212.         {
    213.             while (true )
    214.             {
    215.                 textBox3.Text = x.ToString();
    216.                 Thread.Sleep(100);
    217.                 textBox3.Refresh();
    218.             }
    219.         }
    220.     }
    221. }

    Thats the simple class i have

    VB Code:
    1. using System;
    2.  
    3. namespace project
    4. {
    5.     /// <summary>
    6.     /// Summary description for Class1.
    7.     /// </summary>
    8.     public class Class1
    9.     {
    10.         private string file1Path="";
    11.         private string file2Path ="";
    12.  
    13.         public Class1()
    14.         {      
    15.             //
    16.             // TODO: Add constructor logic here
    17.             //
    18.         }
    19.         public void setPath1(string x)
    20.         {
    21.             file1Path = x;
    22.         }
    23.         public void setPath2(string x)
    24.         {
    25.             file2Path = x;
    26.         }
    27.         public string getPath1()
    28.         {
    29.             return file1Path;
    30.         }
    31.         public string getPath2()
    32.         {
    33.             return file2Path;
    34.         }
    35.     }
    36. }

    I'm using MS Visual Studio .NET 2003

    Thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating textbox with thread?

    I'm not quite sure what you're expecting.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    115

    Re: updating textbox with thread?

    Quote Originally Posted by jmcilhinney
    I'm not quite sure what you're expecting.
    You know like in VB if we have a loop from 0 to 99999999 and when we run it without doevents and would want to see the progress in a textbox, it will only show the last 99999999 number, however if we insert DoEvents inside the loop we can actually see how it counts, that's what I want in C#

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating textbox with thread?

    The insert DoEvents. DoEvents is a method of the Application class and is available in C# just like it is in VB.NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    115

    Re: updating textbox with thread?

    Quote Originally Posted by jmcilhinney
    The insert DoEvents. DoEvents is a method of the Application class and is available in C# just like it is in VB.NET.
    I've read in many places that Doevents is not really a good method of doing this kind of thing, that's why i'm looking for alternative.

    Thanks

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating textbox with thread?

    That's quite correct. DoEvents will check for any pending events at all and it will block until they are all handled. The checking is relatively expensive. If all you want is to update a specific control then you should call Refresh on that control, which is what I advised earlier. I don't see the issue. For example:
    Code:
                for (int i = 0; i < 100; i++)
                {
                    this.label1.Text = i.ToString();
                    this.label1.Refresh();
                    System.Threading.Thread.Sleep(100);
                }
    Last edited by jmcilhinney; Mar 28th, 2006 at 12:24 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width