在C#中使用代理的进度 [英] Progressbarusing delegates in C#

查看:49
本文介绍了在C#中使用代理的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam在c#windows应用程序中使用进度条,在文本框中读取文件iam后,但是进度条工作不正常,进度条保持相同位置,代码执行,请解决此问题。



在gridview_cellclick中选择单元格时执行此操作





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;
            }
        }

推荐答案

因为这就是Invoke的作用:它将代码移回UI线程。这意味着UI线程 - 再次 - 忙于读取文件内容,并且在完成之前无法更新任何进度条。



如果要读取文件并且同时显示进度条,我建议使用BackgroundWorker,它具有内置的进度报告机制来更新进度条(它在UI线程上执行,因此您不需要调用它)。在线程的DoWork处理程序中,以块的形式读取文件,并每10%左右报告一次新的进度以更新该栏。 ReadAllText是一个单片操作,在整个文件完成之前不会返回,因此在整个读取完成之前无法显示进度。
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.


这篇关于在C#中使用代理的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆