Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

hi guys i tried to copy some files with this Code everything is good and the app will copy files but in copy progress i cant move my app or do anything i tried to use thread but its not works i also use backgroundWorker but still nothing the only control that doesnt get stuck is progressBar its works fine here is my code :

 public Form1()
    {
        InitializeComponent();
        backgroundWorker1.Dispose();
        backgroundWorker1.DoWork += BackgroundWorker_DoWork;
        backgroundWorker1.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        backgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
        backgroundWorker1.WorkerReportsProgress = true;

        backgroundWorker2.DoWork += BackgroundWorker2_DoWork;
        backgroundWorker2.WorkerReportsProgress = true;
    }

    private void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        File.Copy(sourcePath, targetPath);
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < fileSize; i++)
        {
            int p = (i + 1) * 100 / Convert.ToInt32(fileSize);
            backgroundWorker1.ReportProgress(p);
        }
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }

    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lbProgress.Text = e.ProgressPercentage.ToString();
        progressBar1.Value = e.ProgressPercentage;
    }

    private void btnTarget_Click(object sender, EventArgs e)
    {
        folderBrowser = new FolderBrowserDialog();
        if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            targetPath += folderBrowser.SelectedPath + @"" + fileName;
            lbTarget.Text = targetPath;
        }
    }

    private void btnSource_Click(object sender, EventArgs e)
    {
        op = new OpenFileDialog();
        if (op.ShowDialog() == DialogResult.OK)
        {
            sourcePath += op.FileName;
            lbSource.Text = sourcePath;
            fileInfo = new FileInfo(sourcePath);
            fileSize = fileInfo.Length / 1024;
            fileName = fileInfo.Name;
            MessageBox.Show(string.Format("File size is: {0} KB", fileSize));
        }
    }

    private void btnCopy_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.RunWorkerAsync();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're updating the progress bar faster than the UI can update, for every single byte of the file being copied in a tight loop. You're flooding the UI thread with pointless work.

Remove backgroundWorker1, it's not doing anything useful anyway. If you don't have a way to track the progress (which you don't with File.Copy), just use a progress bar without progress (set Style to Marquee).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...