• 线程和委托


    #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();
                }
            }
    
  • 相关阅读:
    python读写操作excel数据
    python读写操作excel数据小应用
    操作系统相关知识
    网络编程课堂笔记
    UDP简单初识+socketserver 模块实现服务端并发
    链接循环+模拟远程执行命令+实现大文件上传
    循环通信
    luogu P2761 软件补丁问题
    luogu P4016 负载平衡问题
    P3381 【模板】最小费用最大流(spfa板子)
  • 原文地址:https://www.cnblogs.com/gengyuanchao/p/2827247.html
Copyright © 2020-2023  润新知