C# 可以中斷的多綫程數據備份實例 (同步ManualResetEvent 來控製)

C# 可以中斷的多綫程數據備份實例 (同步ManualResetEvent 來控製):這是工作過程中的寫,有什麽不足的地方請多多指教批評。大家互相進步……

 

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;
using  System.Threading; 

namespace  DataBackup
{
    
public partial class Backup : Form
    
{
        
public Backup()
        
{
            InitializeComponent();

            m_EventStopThread 
= new ManualResetEvent(false);
            m_EventThreadStopped 
= new ManualResetEvent(false);
            m_WorkingFinished 
= new WorkingFinished(this.WorkingThreadFinished);
            m_UpdateInfo 
= new UpdateInfoCallback(this.UpdateInfo);  
        }


        
private void btnCancel_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }


        
private void btnSrcBrowse_Click(object sender, EventArgs e)
        
{
            folderBrowserDialog.ShowNewFolderButton 
= false;
            folderBrowserDialog.Description 
= "Please select source folder you want to copy from";
            
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            
{
                textSource.Text 
= folderBrowserDialog.SelectedPath; 
            }

        }


        
private void btnDstBrowse_Click(object sender, EventArgs e)
        
{
            folderBrowserDialog.ShowNewFolderButton 
= true;
            folderBrowserDialog.Description 
= "Please select destination folder you want to copy to";
            
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            
{
                textDestination.Text 
= folderBrowserDialog.SelectedPath;
            }

        }


        
private void Backup_Load(object sender, EventArgs e)
        
{
            pnlSelect.Visible 
= true;
            pnlProgress.Visible 
= false;
            pnlSelect.Top 
= 0;
            pnlSelect.Left 
= 0;
            
this.Width = pnlSelect.Width + 2 * SystemInformation.BorderSize.Width;
            
this.Height = pnlSelect.Height +  2 * SystemInformation.BorderSize.Height + SystemInformation.CaptionHeight;
        }


        
private void btnOK_Click(object sender, EventArgs e)
        
{
            
if (!Directory.Exists(textSource.Text))
            
{
                textSource.Focus();
                
return;
            }


            
if (textDestination.Text.Length < 3)
            
{
                textDestination.Focus();
                
return;
            }


            
if (!VerifyParameters(textSource.Text, textDestination.Text))
            
{
                textSource.Focus();
                
return;
            }


            pnlSelect.Visible 
= false;
            pnlProgress.Visible 
= true;
            pnlProgress.Top 
= 0;
            pnlProgress.Left 
= 0;
            progressBar.Value 
= 0;
            
this.Width = pnlProgress.Width + 2 * SystemInformation.BorderSize.Width;
            
this.Height = pnlProgress.Height + 2 * SystemInformation.BorderSize.Height + SystemInformation.CaptionHeight;    

            
//create and reset events
            
//moved to part after InitializeComponent();
            
//m_EventStopThread = new ManualResetEvent(false);
            
//m_EventThreadStopped = new ManualResetEvent(false);
            
//m_WorkingFinished = new WorkingFinished(this.WorkingThreadFinished);
            
//m_UpdateProgressbar = new UpdateProgressbarCallback(this.UpdateProgressBar);            m_EventStopThread.Reset();
            m_EventThreadStopped.Reset();
            m_EventThreadStopped.Reset();

            
//Create working thread instance
            m_WorkingThread = new Thread(new ThreadStart(this.StartWorkingThread));
            m_WorkingThread.Name 
= "Data Backup";
            m_WorkingThread.Start();
        }


        
private bool VerifyParameters(string srcFolder, string dstFolder)
        
{
            
//initialize
            if ((srcFolder == ""| (!Directory.Exists(srcFolder)))
                
return false;

            
if (dstFolder == "")
                
return false;

            
if (!Directory.Exists(dstFolder))
            
{
                Directory.CreateDirectory(dstFolder);
                
if (!Directory.Exists(dstFolder))
                    
return false;
            }


            
return true;
        }


        
private void btnAbort_Click(object sender, EventArgs e)
        
{
            
if (MessageBox.Show("Would you like to cancel data backup?""Information", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Information) 
== DialogResult.Yes)
            
{
                StopThread();
                m_WorkingThread 
= null;
                
this.Close();
            }

        }


        
//delegate used to call mainForm functions from other thread
        
//
        
//This delegate enables asynchronous calls for setting
        
//the propertites of Progressbar Control

        
public delegate void WorkingFinished();
        
public delegate void UpdateInfoCallback(string Filename, int maxNum, int curNum);
        
        
//private Worker workerObject = null;
        private Thread m_WorkingThread = null;
        
public WorkingFinished m_WorkingFinished;
        
public UpdateInfoCallback m_UpdateInfo;

        
private ManualResetEvent m_EventStopThread;
        
private ManualResetEvent m_EventThreadStopped;


        
//this function will start after method Thread.Start() is being called;
        private void StartWorkingThread()
        
{
            
//start another thread to start long process
            DataBackupThread myDataBackupThread = new DataBackupThread(m_EventStopThread,
                m_EventThreadStopped, 
this,
                
this.textSource.Text, this.textDestination.Text);

            myDataBackupThread.Working();

        }

        
//clear variable after Thread finished
        
//called from worker thread using delegate and control.Invoke
        
//
        private void WorkingThreadFinished()
        
{
            MessageBox.Show(
"Data backup finished.""Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            m_WorkingThread 
= null;
            
this.Close();
        }

        
        
//called by thread
        
//If the calling thread is the same as the thread that created the control,
        
//the property of control is set directly.
        
//
        
//If the calling thread is different from the thread that created
        
// the control, this method creates a callback and calls itself asynchronously
        
//using the Invoke method
        private void UpdateInfo(string Filename, int maxNum, int curNum)
        
{
            
this.progressBar.Maximum = maxNum;
            
this.progressBar.Value = curNum;
            
this.lbFile.Text = Path.GetFileName(Filename);
        }


        
// Stop worker thread if it is running.
        
// Called when user presses Stop button of form is closed.
        private void StopThread()
        
{
            
if (m_WorkingThread != null && m_WorkingThread.IsAlive)  // thread is active
            {
                
// set event "Stop"
                m_EventStopThread.Set();

                
// wait when thread  will stop or finish
                while (m_WorkingThread.IsAlive)
                
{
                    
// We cannot use here infinite wait because our thread
                    
// makes syncronous calls to main form, this will cause deadlock.
                    
// Instead of this we wait for event some appropriate time
                    
// (and by the way give time to worker thread) and
                    
// process events. These events may contain Invoke calls.
                    if (WaitHandle.WaitAll(
                        (
new ManualResetEvent[] { m_EventThreadStopped }),
                        
100,
                        
true))
                    
{
                        
break;
                    }


                    Application.DoEvents();
                }

            }

        }


        
private void Backup_FormClosing(object sender, FormClosingEventArgs e)
        
{
            StopThread();
        }



    }


    
/// <summary>
    
/// Class handle data backup which runs in working thread
    
/// and makes synchronous user UI operations.
    
/// </summary>

    public class DataBackupThread
    
{
        
Members       

        
Constructor

        
Function Working
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值