博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
listBox1.InvokeRequired学习
阅读量:4116 次
发布时间:2019-05-25

本文共 2545 字,大约阅读时间需要 8 分钟。

控件只能由创建它的线程来访问。其他线程想访问必须调用该控件的Invoke方法。Invoke有两个参数,一个是委托方法,一个是参数值。下面代码就是举例为ListBox添加数据。
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace TestAutoUpEBooks
{
    public partial class Form1 : Form
    {
        delegate void SetListBox(string[] strValues);                  
定义委托
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread tr = new Thread(new ThreadStart(SetListBoxV));  
创建线程
            tr.Start();                                                                             
启动线程
        }
        private void SetListBoxV()
        {
            string[] s = new string[3] {"a","b","c"};
            SetListBoxValue(s);
        }
        private void SetListBoxValue(string[] values)
        {
            if (this.listBox1.InvokeRequired)                         
   当有新工作进程访问控件时InvokeRequired为True
            {
                SetListBox slb = new SetListBox(SetListBoxValue);
定义委托对象
                listBox1.Invoke(slb, new object[] { values});    
用当前工作进程对控件进行访问
        }
            else     
对ListBox添加数据
            {
                for (int i = 0; i < values.Length; i++)
                {
                    listBox1.Items.Add((object)values[i]);
                }
            }
        }
    }
}
这样就实现了新建的工作进程对控件的访问。
多线程可以解决用户响应
namespace
WindowsFormsApplication6
{
   
public
partial
class
Form1 : Form
    {
       
public
Form1()
        {
            InitializeComponent();
        }
       
private
delegate
void
addlistdlg(
string
str);
       
private
Thread mythread;
       
private
void
addlistmethod(
string
mystr)
        {
            listBox1.Items.Add(mystr);
        }
       
private
void
addlistfun()
        {
           
for
(
int
i
=
1
; i
<
90000
; i
++
)
            {
               
string
mystr
=
"
"
+
i.ToString()
+
"
"
;
               
if
(listBox1.InvokeRequired)
                    listBox1.Invoke(
new
addlistdlg(addlistmethod),
new
Object[] { mystr });
               
else
                    listBox1.Items.Add(mystr);
            }
        }
       
private
void
button2_Click(
object
sender, EventArgs e)
//
执行就会卡死
        {
            mythread
=
new
Thread(
new
ThreadStart(addlistfun));
            mythread.Start();
        }
    }
}
if (listBox1.InvokeRequired)
listBox1.Invoke(new addlistdlg(addlistmethod), new Object[] { mystr });
else
listBox1.Items.Add(mystr);
的else部分只是我帮你补完整而已,就是变成如果不是线程里运行也可以执行的通用代码
实例在里这是每次执行
listBox1.Invoke(new addlistdlg(addlistmethod), new Object[] { mystr });
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private delegate void addlistdlg(string str);
        private Thread mythread;
        private void addlistmethod(string mystr)
        {
            listBox1.Items.Add(mystr);
        }
        private void addlistfun()
        {
            for (int i = 1; i < 90000; i++)
            {
                string mystr = "第" + i.ToString() + "个";
                if (listBox1.InvokeRequired)
                    listBox1.Invoke(new addlistdlg(addlistmethod), new Object[] { mystr });
                else
                    listBox1.Items.Add(mystr);
                Thread.Sleep(1000);
            }
        }
        private void button2_Click(object sender, EventArgs e)//执行就会卡死
        {
            mythread = new Thread(new ThreadStart(addlistfun));
            mythread.Start();
        }
    }
}
在循环中加Thread.Sleep(1000);

转载地址:http://zjupi.baihongyu.com/

你可能感兴趣的文章
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
JDBC核心技术 - 下篇
查看>>
一篇搞懂Java反射机制
查看>>
【2021-MOOC-浙江大学-陈越、何钦铭-数据结构】树
查看>>
MySQL主从复制不一致的原因以及解决方法
查看>>
RedisTemplate的key默认序列化器问题
查看>>
序列化与自定义序列化
查看>>
ThreadLocal
查看>>
从Executor接口设计看设计模式之最少知识法则
查看>>
OKhttp之Call接口
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>