我的Winform应用程序的性能问题 [英] Perfomance issue with my Winform Application

查看:107
本文介绍了我的Winform应用程序的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了一个win form应用程序,其中存在一些与性能相关的问题。我的问题如下。



我使用了一个标签控件,其中有四个标签。

从我设置的标签之一我的Csv文件的上传路径。

现在,当我有大量文件(比如说2000 csvs)时,应用程序就会挂起。



我想要做的事情。

1)想要加快csv上传过程。

2)现在当文件上传的时候我应用程序挂起时,无法在我的标签之间移动。所以我需要在文件上传期间移动我的标签。



等待你的回复

Rajan Bajania

Hi All,

I Have created a win form app in which there is some performance related issue. my Issues is as below.

I have used a tab control in which I have Four tabs.
From one of the tab I set the upload path of my Csv Files.
Now when ever I have large amount of file (say 2000 csvs) at that time application get hang.

I want to things to do.
1) Want to speed up the csv uploading process.
2) right now when file are getting uploaded at that time i can't Move between my tabs as application got hang. so I need to move around my tabs during the file uploading take place.

Waiting for your reply
Rajan Bajania

推荐答案

显然最好的解决方案是后台工作人员。

下面复制的是我找到的示例代码,



它根据您的需要使用进度条。后台工作人员在不减慢其他任务的情况下完成此过程。





您可以通过此处的说明解决您的问题/>
http://ntsblog.homedev.com.au/index.php/2012/03/30/background-worker-thread-code-sample-event-handlers-cross-thread-invoke/ [ ^ ]







Obviously the best solution is with a background worker.
Copied below is a sample code i found,

It uses a progress bar as you required. The backgroundworker do the process without slowing the other tasks.


The solution for your problem can be achieved from the explanation in this
http://ntsblog.homedev.com.au/index.php/2012/03/30/background-worker-thread-code-sample-event-handlers-cross-thread-invoke/[^]



using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BackgroundWorkerThreadExample
{
    public partial class Form1 : Form
    {
        public delegate void ProgressUpdatedCallaback(ProgressUpdatedEventArgs progress);
        BackgroundWorker bw = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DatabaseProcessor.ProgressUpdated += new DatabaseProcessor.ProgressUpdatedEvent(ProgressUpdated);
            DatabaseProcessor.GetData();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            bw.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bw.RunWorkerAsync();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bw.IsBusy == true)
            {
                bw.CancelAsync();
            }
            bw.Dispose();
        }

        private void ProgressUpdated(ProgressUpdatedEventArgs progressUpdated)
        {
            if (InvokeRequired)
            {
                Invoke(new ProgressUpdatedCallaback(this.UpdateProgress), new object[] { progressUpdated });
            }
            else
            {
                UpdateProgress(progressUpdated);
            }
        }

        private void UpdateProgress(ProgressUpdatedEventArgs args)
        {
            ProgressBar pb = new ProgressBar();
            Label lb = new Label();

            if (args.Message == "")
            {
                if (args.PBNum == 1)
                {
                    pb = progressBar1;
                    lb = label1;
                }
                else if (args.PBNum == 2)
                {
                    pb = progressBar2;
                    lb = label2;
                }

                if (pb.Maximum != args.Total)
                {
                    // initial setup
                    pb.Minimum = 0;
                    pb.Maximum = args.Total;
                    pb.Style = ProgressBarStyle.Continuous;
                }

                pb.Value = args.Processed;

                if (args.Total > 0)
                {
                    double progress = args.Processed / (args.Total * 1.0);
                    lb.Text = progress.ToString("P2");
                }
            }
            else
            {
                this.richTextBox1.Text += args.Message;
                //Goto last line
                this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
                this.richTextBox1.ScrollToCaret();
            }
            //Application.DoEvents();
        }
    }

    public static class DatabaseProcessor
    {
        public delegate void ProgressUpdatedEvent(ProgressUpdatedEventArgs progressUpdated);
        public static event ProgressUpdatedEvent ProgressUpdated;

        public static void GetData()
        {
            int total = 126;
            Random randomGenerator = new Random();
            for (int i = 0; i < total; i++)
            {
                // Do some processing here
                double delay = (double)randomGenerator.Next(2) + randomGenerator.NextDouble();
                int sleep = (int)delay * 1000;
                System.Threading.Thread.Sleep(sleep);
                RaiseEvent(1, total, i + 1);
                RaiseEvent(0, 0, 0, string.Format("Processing Item {0} \r\n", i + 1));

                for (int ii = 0; ii < total; ii++)
                {
                    // Do some processing here
                    double delay2 = (double)randomGenerator.Next(2) + randomGenerator.NextDouble();
                    int sleep2 = (int)delay2 * 10;
                    System.Threading.Thread.Sleep(sleep2);
                    RaiseEvent(2, total, ii + 1);
                }
            }
        }

        private static void RaiseEvent(int pbNum, int total, int current, string message = "")
        {
            if (ProgressUpdated != null)
            {
                ProgressUpdatedEventArgs args = new ProgressUpdatedEventArgs(pbNum, total, current, message);
                ProgressUpdated(args);
            }
        }
    }

    public class ProgressUpdatedEventArgs : EventArgs
    {
        public ProgressUpdatedEventArgs(int pbNum, int total, int progress, string message = "")
        {
            this.PBNum = pbNum;
            this.Total = total;
            this.Processed = progress;
            this.Message = message;
        }
        public string Message { get; private set; }
        public int PBNum { get; private set; }

        public int Processed { get; private set; }
        public int Total { get; private set; }
    }
}


这篇关于我的Winform应用程序的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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