猿问

ProgressBar 窗体 c#

有一个使用 BinaryFormatter 序列化多个表的大型错误代码如何更新进度条?


执行序列化的代码是:


 private void Serialize()

    {

        try

        {


            int numberTableProcessed = 0;


            txtBoxProcessing.Text += "Start ..." + DateTime.Now + Environment.NewLine;



            AGENTS agenti = new AGENTS();

            agenti.SelectAndSerializeAgenti(openedConn);

            string pathAgenti = @"C:\...AGENTS.bin";

            if (File.Exists(pathAgenti))

            {

                long size = new FileInfo(pathAgenti).Length;

                 txtBoxProcessing.Text += "Processed AGENTI - " + string.Format("byte: {0:n}", size) + Environment.NewLine;

            }

            numberTableProcessed++;

            UpdateProgressBar(numberTableProcessed);


            PRODUCT anaart = new PRODUCT();

            anaart.SelectAndSerializeAnaart(openedConn);

            string pathAnaart = @"C:\...PRODUCT.bin";

            if (File.Exists(pathAnaart))

            {

                long size = new FileInfo(pathAnaart).Length;

                 txtBoxProcessing.Text += "Processed PRODUCT - " + string.Format("byte: {0:n}", size) + Environment.NewLine;

            }

            numberTableProcessed++;

            UpdateProgressBar(numberTableProcessed);

            ...

}


更新进度条的代码


private void UpdateProgressBar(int numberTableProcessed)

    {

        int numberTotalTables = 22;

        progressBar1.Value = (numberTableProcessed / numberTotalTables) * 100;

    }

使用方法 UpdateProgressBar 我看不到进度......最后它变成全绿色,因为代码正在运行(我认为)


如何更新进度条?


我根据路西法的建议尝试这个


public Form1()

    {

        InitializeComponent();


        backgroundWorker1.DoWork += backgroundWorker1_DoWork;

    }


    public void PerformSerialize()

    {

        backgroundWorker1.RunWorkerAsync();

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

    {

        Serialize();

    }




MYYA
浏览 110回答 1
1回答
随时随地看视频慕课网APP
我要回答