线程和委托

#region 线程
        //线程一的方法
        private void CrossThreadFlush()
        {
            while (true)
            {
                if (bFirstThread)
                {
                    continue;
                }
                else
                {
                    //将代理绑定到方法 
                    FlushClient fc = new FlushClient(threadFunFirst);
                    //this.BeginInvoke(fc);//调用代理
                    lvListView.Invoke(fc);
                    bFirstThread = true;
                }
            }
        }

        //线程一的方法:开启ListView的虚拟模式
        private void threadFunFirst()
        {
            //虚拟模式绑定
            strList.Clear();
            //imageList1.Images.Clear();
            if(CurrentCacheItemSource != null)
            {
                CurrentCacheItemSource = null; 
            }
            string[] astrPath = Directory.GetFileSystemEntries(tNode.Name);

            foreach (string strPath in astrPath)
            {
                if (clManageFile.IsHidden(strPath))
                {
                    continue;
                }
                strList.Add(strPath);
            }
            lvListView.VirtualListSize = strList.Count;
            lvListView.VirtualMode = true;
        }

        //线程二的方法:控制线程一
        private void threadFunSecond()
        {
            while (true)
            {
                try
                {
                    Monitor.Enter(list);
                    iLoopx = list.Count;
                }
                finally
                {
                    Monitor.Exit(list);
                }
                Thread.Sleep(100);
                try
                {
                    Monitor.Enter(list);
                    iLoopy = list.Count;
                }
                finally
                {
                    Monitor.Exit(list);
                }
                if (iLoopx != iLoopy)
                {
                    continue;
                }
                if (tNode != null)
                {
                    if (tNode.Checked)
                    {
                        bFirstThread = false;
                        list.Clear();
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
ListBox listbox;
        StreamWriter sw;
        public Service(ListBox listbox, StreamWriter sw)
        {
            this.listbox = listbox;
            this.sw = sw;
        }
        public void SendToServer(string str)
        {
            try
            {
                sw.WriteLine(str);
                sw.Flush();
            }
            catch
            {
                SetListBox("发送数据失败");
            }
        }
        delegate void ListBoxCallback(string str);
        public void SetListBox(string str)
        {
            if (listbox.InvokeRequired == true)
            {
                ListBoxCallback d = new ListBoxCallback(SetListBox);
                listbox.Invoke(d, str);
            }
            else
            {
                listbox.Items.Add(str);
                listbox.SelectedIndex = listbox.Items.Count - 1;
                listbox.ClearSelected();
            }
        }

 

posted on 2012-12-20 22:53  紫雨心  阅读(149)  评论(0编辑  收藏  举报

导航