c#从另一个类更新进度条

时间:2015-08-07 13:23:51

标签: c# progress-bar backgroundworker

首先,我要打招呼,我是新来的,在c#;)

我正在编写程序,用于在excel和SQLite文件之间传输翻译。这需要一段时间,因此我尝试使用progressbar,它是由Backgroundworker(在backgorundworker1_DoWork中)转移类(Imp / Exp)更新的。我正在尝试使用此答案report progress backgroundworker from different class c#,但条形图仅在启动时和传输完成后更新。表格似乎有用,我可以移动它们等等;我尝试了第二个解决方案,有一个事件,但它的工作方式相同;

有一个可能重要的代码

表单类:

public partial class Form2 : Form
{

    private int process = 0;
    private ImpExp prgr;
    private bool _overwrite;
    public Form1 form1;
    public bool overwrite
    {
        get
        {
            return _overwrite;
        }
         private set
        {
            _overwrite = value;
            prgr.overwrite = _overwrite;
        }

    }
    public Form2(Form1 firstForm)
    {
        prgr = new ImpExp(firstForm.excPath, firstForm.excelCol, firstForm.dbPath, firstForm.otherLanguage, this);
        this.form1 = firstForm;
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = false;
        this.Closing += new System.ComponentModel.CancelEventHandler(this.Form2_Closing);
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

    }

    public void addColumn()
    {
        prgr.DB.addColumn();
    }

    private void importButton_Click(object sender, EventArgs e)
    {
            process = 1;
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
    }

    private void exportButton_Click(object sender, EventArgs e)
    {

        if (!backgroundWorker1.IsBusy)
            backgroundWorker1.RunWorkerAsync();
        process = 2;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        YesRadioButton.Checked = true;
        AddLanguageForm addLanguageForm = new AddLanguageForm(this);
        if (!prgr.isReady)
        {
            Close();
        }

        else if (!prgr.DB.langGood)
            {

                addLanguageForm.ShowDialog();
                if (!addLanguageForm.yes)
                {
                    Close();
                }
            }
        Console.WriteLine("Form2 Opened");
        overwrite = YesRadioButton.Checked;
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text=(e.ProgressPercentage.ToString() + "%");

        Console.WriteLine("ProgressChanged " + e.ProgressPercentage);

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        if (process == 1)   //import
        {
            base.Invoke((Action)delegate
            {
                cancelButton.Visible = false;

            });

            prgr.Import(sender as BackgroundWorker);
            process = 0;

            base.Invoke((Action)delegate
            {

                this.cancelButton.Text = "OK";
                cancelButton.Visible = true;
            });


        }
        else if (process == 2)  //export
        {
            base.Invoke((Action)delegate
            {
                cancelButton.Visible = false;
            });

            prgr.Export(sender as BackgroundWorker);
            process = 0;

            base.Invoke((Action)delegate
            {

                this.cancelButton.Text = "OK";
                cancelButton.Visible = true;
            });

        }

    }


    private void YesRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (YesRadioButton.Checked == true)
        {
            NoRadioButton.Checked = false;
        }
        overwrite = YesRadioButton.Checked;
    }

    private void NoRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (NoRadioButton.Checked == true)
        {
            YesRadioButton.Checked = false;
        }
        overwrite = YesRadioButton.Checked;

    }


}

我试图实现进度条的Imp / Exp类:

class ImpExp
{
    internal MyExcel excel;
    internal MySQLite DB;
    private Form2 form2;
    int firstRow, lastRow;
    internal bool overwrite;

    public bool isReady
    {
        get
            {
                if (excel.isReady && DB.isReady)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        set
            {
            }
    }
    public ImpExp(string excelPath, int excelCol, string dBPath, string dBCol, Form2 dad)
    {
        form2 = dad;
        excel = new MyExcel(excelPath, excelCol);
        DB = new MySQLite(dBPath,dBCol);
        firstRow = 6;
        lastRow=excel.lastRow;


    }
    public void Finish()
    {
        excel.close();
        DB.close();
    }
    public void Export(BackgroundWorker bw)
    {

        bw.WorkerSupportsCancellation = true;
        bw.WorkerReportsProgress = true;
        ExcelRow row = new ExcelRow();
        for (int i = firstRow; i <= excel.lastRow; i++)
        {

            excel.rowNumber = i;
            row = excel.actualRow();
            if (row.pl != null || row.other != null)
            DB.actualizeDB(row);
            Console.WriteLine(row.pl);
            Console.WriteLine(row.other);
            bw.ReportProgress(i/lastRow*100);

        }
        MessageBox.Show("Export finished");

    }
    public void Import(BackgroundWorker bw)
    {
        bw.WorkerSupportsCancellation = true;
        bw.WorkerReportsProgress = true;

        ExcelRow row = new ExcelRow();
        for (int i = firstRow; i <= excel.lastRow; i++)
        {
            string translationTemp;
            excel.rowNumber = i;
            row = excel.actualRow();
            if (overwrite)
            {
                if (row.pl != null)   //pl for Polish
                {
                    translationTemp=DB.translate(row.pl);
                    if (translationTemp != "TRANSLATION NOT FOUND!@#$%")       
                    {
                        string translationTemp2 = translationTemp.Replace(" ","");
                        if(translationTemp2 !="")       //Nie nadpisuj excela pustymi wpisami i spacjami
                        {
                            excel.saveTranslation(translationTemp, i);
                        }
                    }
                }
            }
            else
            {
                if (row.pl != null && (row.other == null || row.other == ""))
                {
                    translationTemp = DB.translate(row.pl);
                    if (translationTemp != "TRANSLATION NOT FOUND!@#$%")
                    {
                        excel.saveTranslation(translationTemp, i);
                    }
                }
            }
            bw.ReportProgress(i / lastRow * 100);



        }
        MessageBox.Show("Import finished");
    }


}

1 个答案:

答案 0 :(得分:0)

如果你想改变Wfa的任何控制权。你必须使用某种synchonizationContext。简单就是。

structure(list(X = 1:50, Sample = structure(c(1L, 12L, 23L, 34L, 
45L, 47L, 48L, 49L, 50L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 
40L, 41L, 42L, 43L, 44L, 46L), .Label = c("Sample 1", "Sample 10", 
"Sample 11", "Sample 12", "Sample 13", "Sample 14", "Sample 15", 
"Sample 16", "Sample 17", "Sample 18", "Sample 19", "Sample 2", 
"Sample 20", "Sample 21", "Sample 22", "Sample 23", "Sample 24", 
"Sample 25", "Sample 26", "Sample 27", "Sample 28", "Sample 29", 
"Sample 3", "Sample 30", "Sample 31", "Sample 32", "Sample 33", 
"Sample 34", "Sample 35", "Sample 36", "Sample 37", "Sample 38", 
"Sample 39", "Sample 4", "Sample 40", "Sample 41", "Sample 42", 
"Sample 43", "Sample 44", "Sample 45", "Sample 46", "Sample 47", 
"Sample 48", "Sample 49", "Sample 5", "Sample 50", "Sample 6", 
"Sample 7", "Sample 8", "Sample 9"), class = "factor"), Time = structure(1:50, .Label = c("10:51:04 AM", 
"10:51:05 AM", "10:51:06 AM", "10:51:07 AM", "10:51:08 AM", "10:51:09 AM", 
"10:51:10 AM", "10:51:11 AM", "10:51:12 AM", "10:51:13 AM", "10:51:14 AM", 
"10:51:15 AM", "10:51:16 AM", "10:51:17 AM", "10:51:18 AM", "10:51:19 AM", 
"10:51:20 AM", "10:51:21 AM", "10:51:22 AM", "10:51:23 AM", "10:51:24 AM", 
"10:51:25 AM", "10:51:26 AM", "10:51:27 AM", "10:51:28 AM", "10:51:29 AM", 
"10:51:30 AM", "10:51:31 AM", "10:51:32 AM", "10:51:33 AM", "10:51:34 AM", 
"10:51:35 AM", "10:51:36 AM", "10:51:37 AM", "10:51:38 AM", "10:51:39 AM", 
"10:51:40 AM", "10:51:41 AM", "10:51:42 AM", "10:51:43 AM", "10:51:44 AM", 
"10:51:45 AM", "10:51:46 AM", "10:51:47 AM", "10:51:48 AM", "10:51:49 AM", 
"10:51:50 AM", "10:51:51 AM", "10:51:52 AM", "10:51:53 AM"), class = "factor"), 
    L = c(57.61, 57.16, 53.96, 57.3, 55.27, 57.9, 59.05, 55.13, 
    53.8, 57.59, 52.23, 57.93, 58.59, 56.27, 58.62, 61.25, 56.76, 
    56.64, 58.49, 53.99, 53.17, 56.77, 57.35, 53.43, 55.19, 54.5, 
    53.17, 53.88, 55.15, 61.81, 57.03, 55.97, 54.83, 59.53, 54.29, 
    56.84, 53.53, 55.38, 57.84, 58.32, 54.67, 52.72, 53.94, 55.17, 
    58.15, 53.55, 58.75, 56.07, 58.46, 60.33), C = c(4.56, 4.17, 
    5.14, 3.9, 3.63, 3.47, 4.3, 4.95, 5.76, 3.49, 4.7, 4.64, 
    5.64, 3.76, 2.25, 4.66, 5.96, 4.13, 5.32, 4.45, 4.11, 3.88, 
    5.47, 4.17, 5.92, 2.71, 5.2, 4.24, 5.78, 5.37, 4.71, 4.39, 
    3.83, 5.01, 4.62, 5.08, 4.74, 3.62, 3.59, 4.09, 3.32, 4.06, 
    4.09, 5.16, 3.1, 5.59, 3.06, 3.67, 4.56, 6.75), h = c(219.98, 
    226.13, 233.39, 221.78, 213.56, 214.16, 230.93, 229.57, 236.17, 
    230.59, 235.2, 237.58, 240.43, 228.53, 206.55, 234.13, 241.12, 
    231.82, 227.03, 231.32, 218.68, 230.31, 228.59, 229.36, 235.56, 
    197.32, 232.49, 228.39, 244.63, 235.78, 231.22, 228.92, 231.17, 
    244.2, 228.49, 234.01, 227.85, 226.29, 210.04, 232.38, 222.71, 
    220.69, 226.08, 233.15, 215.76, 236.26, 206.3, 219.79, 232.37, 
    246.63), L1 = c(57.61, 57.16, 53.96, 57.3, 55.27, 57.9, 59.05, 
    55.13, 53.8, 57.59, 52.23, 57.93, 58.59, 56.27, 58.62, 61.25, 
    56.76, 56.64, 58.49, 53.99, 53.17, 56.77, 57.35, 53.43, 55.19, 
    54.5, 53.17, 53.88, 55.15, 61.81, 57.03, 55.97, 54.83, 59.53, 
    54.29, 56.84, 53.53, 55.38, 57.84, 58.32, 54.67, 52.72, 53.94, 
    55.17, 58.15, 53.55, 58.75, 56.07, 58.46, 60.33), a = c(-3.49, 
    -2.89, -3.06, -2.91, -3.03, -2.87, -2.71, -3.21, -3.21, -2.22, 
    -2.68, -2.49, -2.79, -2.49, -2.01, -2.73, -2.88, -2.55, -3.63, 
    -2.78, -3.21, -2.48, -3.62, -2.72, -3.35, -2.58, -3.16, -2.81, 
    -2.48, -3.02, -2.95, -2.88, -2.4, -2.18, -3.06, -2.99, -3.18, 
    -2.5, -3.11, -2.5, -2.44, -3.08, -2.84, -3.1, -2.51, -3.1, 
    -2.74, -2.82, -2.79, -2.68), b = c(-2.93, -3.01, -4.13, -2.6, 
    -2.01, -1.95, -3.34, -3.77, -4.79, -2.7, -3.86, -3.92, -4.91, 
    -2.82, -1, -3.78, -5.22, -3.24, -3.89, -3.47, -2.57, -2.99, 
    -4.11, -3.17, -4.88, -0.81, -4.12, -3.17, -5.22, -4.44, -3.67, 
    -3.31, -2.99, -4.51, -3.46, -4.11, -3.52, -2.61, -1.8, -3.24, 
    -2.25, -2.64, -2.94, -4.13, -1.81, -4.65, -1.35, -2.35, -3.61, 
    -6.2), DE = c(36.52, 36.95, 40.24, 36.78, 38.77, 36.13, 35.08, 
    39.04, 40.47, 36.47, 41.92, 36.24, 35.72, 37.8, 35.35, 32.94, 
    37.57, 37.46, 35.74, 40.14, 40.9, 37.31, 36.9, 40.67, 39.11, 
    39.48, 41.02, 40.22, 39.15, 32.48, 37.13, 38.15, 39.24, 34.72, 
    39.84, 37.37, 40.61, 38.67, 36.2, 35.79, 39.35, 41.35, 40.14, 
    39.03, 35.86, 40.7, 35.25, 37.98, 35.69, 34.2), heihgtmm = c(53.1, 
    67.01, 80.16, 85.3, 86.37, 92.36, 91.12, 90.56, 91.02, 94.25, 
    96.79, 94.36, 96.26, 95.4, 93, 91.58, 91.92, 89, 81.52, 68.57, 
    53.54, 49.43, 54.31, 72.51, 81.95, 82.85, 86.77, 85.47, 90.1, 
    87.95, 90.1, 86.97, 89.42, 89.65, 87.56, 83.48, 76.43, 63.15, 
    54.92, 64.79, 67.47, 64.77, 61.6, 63.58, 69.27, 79.75, 83.41, 
    85.56, 88.47, 90.57), hex = structure(c(1L, 1L, 1L, 1L, 3L, 
    3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L
    ), .Label = c("", "#507085", "#8A8F8C"), class = "factor")), .Names = c("X", 
"Sample", "Time", "L", "C", "h", "L1", "a", "b", "DE", "heihgtmm", 
"hex"), class = "data.frame", row.names = c(NA, -50L))

当您需要更改控件中的某些内容时,例如label1。你必须这样做

    private void Change<T>(T obj, Action<T> action)
        where T : Control
    {
        if (obj.InvokeRequired)
        {
            obj.Invoke(
                new MethodInvoker(() => Change(obj, action)),
                new object[] { null });
        }
        else
        {
            action(obj);
        }
    }

第一个参数是你的控件,第二个参数是你的控件。 有关InvokeRequired的更多信息 https://msdn.microsoft.com/en-us/library/ms171728(v=vs.85).aspx