1

我编写了一个代码来将文本文件中的值传输到数据表。由于记录数量很多,我实现了一个进度条来查看状态。但它会引发一个错误,称为

“‘1’的值对‘值’无效。‘值’应该在‘最小值’和‘最大值’之间。”

你能帮我在循环中成功实现进度条吗?这是我使用的代码:

我已经用后台工作人员更新了我的编码。但它会引发一个错误,称为:

“创建窗口句柄时出错”

private void button1_Click(object sender, EventArgs e)
    {            
        OpenFileDialog thisDialog = new OpenFileDialog();
        DataTable dt = new DataTable();
        DataRow dr = null;            

        if (thisDialog.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text=thisDialog.FileName;
            string file1 = textBox1.Text;
            //background worker
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

            if (backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync();
            }

            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                backgroundWorker1.CancelAsync();
            } 
            using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
            {           
                string line=String.Empty;
                int lineno = 0;                    

                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("DISKXFER"))
                    {
                        string dataLine = line.ToString();
                        string[] split = dataLine.Split(',');
                        int result = split.Length;
                        if (lineno == 0)
                        {
                            for (int x = 0; x < result; x++)
                            {
                                DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
                                dt.Columns.Add(dcss);
                            }
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                                
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }                                
                        }                                
                        else
                        {
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                               
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }

                        }                            
                        lineno += 1;                        

                    }

                }

            }
        }

        dataGridView1.DataSource = dt;
    } 

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {            
        string line;
        string file1 = textBox1.Text;
        using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
        {
            int count = 0;
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains("DISKXFER"))
                {
                    backgroundWorker1.ReportProgress(count);
                }
                count += 1;
            }
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
4

4 回答 4

2

如果您希望您的应用程序在循环中不处于“无响应”状态,那么您可以使用后台工作程序。但小心点。文件对话框必须在线程之外,以免出现任何跨线程异常!将文件路径保存到私有字段并在后台工作人员中使用。每次存储一行时,请使用 ReportProgress 以更新进度条!以使用 Background Worker 为例:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

private BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

if (bw.IsBusy != true)
 {
      bw.RunWorkerAsync();
 }
 if (bw.WorkerSupportsCancellation == true)
  {
      bw.CancelAsync();
  }
 private void bw_DoWork(object sender, DoWorkEventArgs e){ }
 private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){ }
 private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e){ }

编辑:

private string file1;
private DataTable dt;
private int iTotalLinesOfFile;
private void button1_Click(object sender, EventArgs e)
    {            
        OpenFileDialog thisDialog = new OpenFileDialog();

        if (thisDialog.ShowDialog() == DialogResult.OK)
        {
            if(dt==null) 
            {
                dt== new DataTable();
            }
            else
            {
                dt.Clear();
            }
            file1 = thisDialog.FileName;
            textBox1.Text=file1;

            iTotalLinesOfFile = System.IO.File.ReadAllLines(file1).Length;
            //background worker
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

            if (backgroundWorker1.IsBusy != true)
            {
                backgroundWorker1.RunWorkerAsync(); //when you call this the DoWork will start and the code will continue!!
            }
            //do not let the user to click the button again!!
            button1.Enabled = false;
            //you will use this code if you want to cancel the job that Worker does.
            //if (backgroundWorker1.WorkerSupportsCancellation == true)
            //{
            //    backgroundWorker1.CancelAsync();
            //} 

        }
    } 

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {   
        DataRow dr = null;   
        using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
            {           
                string line=String.Empty;
                int lineno = 0;                    
                int count = 0;
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("DISKXFER"))
                    {
                        backgroundWorker1.ReportProgress(count);
                        string dataLine = line.ToString();
                        string[] split = dataLine.Split(',');
                        int result = split.Length;
                        if (lineno == 0)
                        {
                            for (int x = 0; x < result; x++)
                            {
                                DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
                                dt.Columns.Add(dcss);
                            }
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                                
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }                                
                        }                                
                        else
                        {
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                               
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }

                        }                            
                        lineno += 1;                        

                    }
                    count += 1;
                }

            }
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        dataGridView1.DataSource = dt;
        button1.Enabled = true;
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        int iCount = e.ProgressPercentage;
        if(iTotalLinesOfFile==0) return;
        //progressBar1.Value must not be less than 0 and more than 100
        progressBar1.Value = (iCount / iTotalLinesOfFile) * 100;
    }
于 2013-05-25T16:31:55.973 回答
1

progressBar1.maximum 目前设置为零!

  • 选项 1:定义 progressBar1 的最大值。

    progressBar1.Maximum = File.ReadAllLines("file.txt").Length;

  • 选项 2:使用Endlessprogressbar来实现您的目标。

编辑:包括选项1的更正代码:

private void button1_Click(object sender, EventArgs e)
    {            
        OpenFileDialog thisDialog = new OpenFileDialog();
        DataTable dt = new DataTable();
        DataRow dr = null;
        progressBar1.Minimum = 0;
        progressBar1.Step = 1;
        progressBar1.Visible = true;

        if (thisDialog.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text=thisDialog.FileName;
            string file1 = textBox1.Text;
            progressBar1.Maximum = File.ReadAllLines(file1).Length;
            using (System.IO.StreamReader file = new System.IO.StreamReader(file1))
            {           
                string line=String.Empty;
                int lineno = 0;                    

                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("DISKXFER"))
                    {
                        string dataLine = line.ToString();
                        string[] split = dataLine.Split(',');
                        int result = split.Length;
                        if (lineno == 0)
                        {
                            for (int x = 0; x < result; x++)
                            {
                                DataColumn dcss = new DataColumn(x.ToString(), Type.GetType("System.String"));
                                dt.Columns.Add(dcss);
                            }
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                                
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }                                
                        }                                
                        else
                        {
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);                                    
                            }
                            dr = dt.Rows[lineno];                               
                            for (int x = 0; x < result; x++)
                            {
                                dr[x+1] = split[x];
                            }

                        }
                        progressBar1.Value = dt.Rows.IndexOf(dr);
                        Application.DoEvents();
                        lineno += 1;                        

                    }

                }

            }
        }

        dataGridView1.DataSource = dt;
    }  

正如其他人已经指出的那样:Backgroundworker 将是一个更好的选择。特别是如果您的 UI 仍然需要响应。

于 2013-05-25T16:22:58.860 回答
0

简单地说,在您分配这个时,数据表中没有行,因此最大值为 0。

progressBar1.Maximum = dt.Rows.Count;

无论如何,由于您似乎事先不知道行数,因此显示任何“进度”没有任何意义,因为当您扩展最大值时它总是会满的。但是,您可以使用选取框模式。或者使用您要添加的行数预先初始化最大值。

于 2013-05-25T16:25:36.930 回答
0

您应该设置您想要的范围ProgressBar.Maximum和属性。ProgressBar.Minimum

此属性获取或设置控件范围的最大值和最大值。它们的默认值0100.

对于Minimum财产;

此属性指定 Value 属性的下限。更改 Minimum 属性的值时,将重绘 ProgressBar 控件以反映控件的新范围。当 Value 属性的值等于 Minimum 属性的值时,进度条为空。要更改进度条的值,请使用 Step 属性和 PerformStep 方法,使用 Increment 方法,或直接设置 Value 属性的值。

于 2013-05-25T16:30:47.090 回答