Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:54:34+00:00 2026-05-25T18:54:34+00:00

I have an a windows form application that use one class (its name is

  • 0

I have an a windows form application that use one class (its name is Parser)
this form has a button and when i click on the windows form application button it call one of the parser class method .

this method simply read text file line after line and write each line to separate file…

i would like to add a progress bar to the form in order to show the progress( it is a very large file )

any idea how to do that? I have in the Parse class 2 property one for the number of line in the file and the second how much lines already checked.

here is my button2_Click function

private void button2_Click(object sender, EventArgs e)
{
      if (this.textBox1 != null & this.textBox2 != null)
      {
           inst.init(this.textBox1.Text, this.textBox2.Text);
           //this.progressBar1.Show();

           inst.ParseTheFile();
           System.Windows.Forms.MessageBox.Show("Parsing finish successfully"); 
       }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:54:34+00:00Added an answer on May 25, 2026 at 6:54 pm

    You could do:

    private void button1_Click(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem((obj) =>
        {
            var lines = File.ReadLines(@"D:\test.txt");
            var count = lines.Count();
    
            for(int i = 0; i < count; i++)
            {
                // some parse work
                Invoke(new Action(() =>
                {
                    progressBar1.Value = (i * 100) / count;
                }));
            }
        });
    }
    

    In the example above, it simply creates a background thread in order not to block the UI thread, until the Invoke method is called.
    The Invoke method is necessary, in order to manipulate with a Control that the current thread isn’t the owner of. It takes a delegate, and runs this delegate on the thread that owns the Control.

    You could even go as far, as making the foreach loop parallel, if it’s a time consuming task to parse the lines. An example:

    private void button1_Click(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem((obj) =>
        {
            var lines = File.ReadLines(@"D:\test.txt");
            var count = lines.Count();
    
            Parallel.For(0, count, i =>
            {
                // some parse work
                Invoke(new Action(() =>
                {
                    progressBar1.Value = (i * 100) / count;
                }));
            });
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows application that has 2 Forms . From one form I
I have a windows form application that uses a Shared class to house all
I have a C# Windows Form application that contains a menu with this event:
I have a c# windows form application that has a similar GUI functionality as
I have a Windows form application that's consuming web services over https and is
I am using windows application and I have four combo boxes(comboeventname,combosendtype,comboType,comboschedule) in that form....
I have a Windows Form application. Version 1 of my app has been released
I have this application (Windows form application written in Visual C++) from a colleague
I have one main windows form and within that form I have custom controls
I have a Service application that crashes only on one PC (Windows XP) when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.