• 线程和委托


    #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();
                }
            }
    
  • 相关阅读:
    WPF数据爬取小工具-某宝推广位批量生成,及订单爬取 记:接单最痛一次的感悟
    .net core2.1
    Ng Alain使用
    MediatR
    RN错误随笔
    【转载】DDD分层架构的三种模式
    1.RN环境搭建,创建项目,使用夜神模拟调试
    ExtJs4 笔记(2) ExtJs对js基本语法扩展支持
    ExtJs4 笔记(1) ExtJs大比拼JQuery:Dom文档操作
    vue的父子通信
  • 原文地址:https://www.cnblogs.com/gengyuanchao/p/2827247.html
Copyright © 2020-2023  润新知