WPF 多线程处理(3)

 

 

 

 

首先我们需要几个属性来保存取得的数据,因为在其他的线程中是不能读取控件的值.

        private string folderPath;
        private string[] files;
        private List<object> listItem;
View Code

 

再申明三个委托来分别执行这些数据,获取路径后读取路径下所有子文件, 更新到listbox上, 然后就是处理processBar.

        private Thread UpdateList = null;
        private Thread UpdateFolder = null;
        private Thread UpdatePBar = null;

        private delegate void UpdateListUI(object ob);
        private delegate void UpdateFolderPath(string path);
        private delegate void UpdatePBarUI(object obj);
View Code

 

然后注册事件:

        public MainWindow()
        {
            listItem = new List<object>();

            InitializeComponent();

            this.MouseDown+=(s,e)=>{
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    this.Cursor = System.Windows.Input.Cursors.Cross;
                    this.DragMove();
                }
            };
            this.MouseUp += (s, e) => {
                this.Cursor = System.Windows.Input.Cursors.Arrow;
            };

            btn_Top.Click += (s, e) => {
                if (this.Topmost == true)
                {
                    this.Topmost = false;
                }
                else
                {
                    this.Topmost = true;
                }
            };

            btn_Min.Click += (s, e) => {
                this.WindowState = WindowState.Minimized;
            };

            btn_Close.Click += (s, e) =>
            {
                this.Close();
            };

            btn_Broswer.Click += (s, e) => {

                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.RootFolder = System.Environment.SpecialFolder.DesktopDirectory;
                fbd.ShowDialog();
                StartBrowser(fbd.SelectedPath);
            };

            btn_Start.Click += (s, e) => {
                UpdateList = new Thread(DoWork);
                if (UpdateList.ThreadState == ThreadState.Running)
                {
                    UpdateList.Abort();
                }
                UpdateList.Start();
            };

            this.listbox1.SelectionChanged += (s, e) => {
                var lbi = this.listbox1.SelectedItem;
                View view = new View(lbi);
                view.ShowDialog();
            };

            this.Closing += (s, e) => {
                StopWork();
            };
        }
View Code

 

下一篇:WPF 多线程处理(4)
上一篇:WPF 多线程处理(2)

 

posted @ 2013-08-18 15:19  风清扬 No.1  阅读(2074)  评论(4编辑  收藏  举报