Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam working with progress-bar in c# windows application, after reading file from text iam appending in textbox, but progress-bar is not working properly, progress-bar remains in same position, and code gets execute, kindly solve this problem.

iam doing this operation when selecting cell in gridview_cellclick


public static Thread thread1;
        public string strSelectedfile;

        private void GridView_integration_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    strSelectedfile = Application.StartupPath + "\\" + GridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                    txtReadfile.Text = string.Empty;
                    progressBar.Visible = true;
                    progressBar.Style = ProgressBarStyle.Marquee;
                    thread1 = new Thread(new ThreadStart(loadTextfile));
                    thread1.Start();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }
        private void loadTextfile()
        {
            setTextSource(strSelectedfile);
        }
        internal delegate void SetDelegate(string textvalue);

        private void setTextSource(string textvalue)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetDelegate(setTextSource), textvalue);
            }
            else
            {
                txtReadfile.Text = File.ReadAllText(textvalue);
                thread1.Abort();
                progressBar.Visible = false;
            }
        }


What I have tried:

public static Thread thread1;
        public string strSelectedfile;

        private void GridView_integration_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    strSelectedfile = Application.StartupPath + "\\" + GridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                    txtReadfile.Text = string.Empty;
                    progressBar.Visible = true;
                    progressBar.Style = ProgressBarStyle.Marquee;
                    thread1 = new Thread(new ThreadStart(loadTextfile));
                    thread1.Start();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }
        private void loadTextfile()
        {
            setTextSource(strSelectedfile);
        }
        internal delegate void SetDelegate(string textvalue);

        private void setTextSource(string textvalue)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetDelegate(setTextSource), textvalue);
            }
            else
            {
                txtReadfile.Text = File.ReadAllText(textvalue);
                thread1.Abort();
                progressBar.Visible = false;
            }
        }
Posted
Updated 1-Nov-17 4:35am

1 solution

Because that's what Invoke does: it moves the code back to the UI thread. Which means the UI thread is - once again - busy reading the file content and can't update any progress bar until it's complete.

If you want to read a file and show a progress bar at the same time, I would suggest using a BackgroundWorker which has a built in progress reporting mechanism to update your progress bar (it's executed on the UI thread so you don't need to invoke that). In your DoWork handler for the thread, read the file in chunks, and report new progress every 10% or so to update the bar. ReadAllText is a "monolithic" operation which doesn't return until the whole file is complete, so progress cannot be shown until the whole read is finished.
 
Share this answer
 
Comments
Vinodh Muthusamy 2-Nov-17 1:16am    
I will accept your solution, do you have any sample code of it
OriginalGriff 2-Nov-17 3:26am    
The Microsoft documentation includes sample code:
https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

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