Winform 子线程更新 控件
来源:互联网 发布:安全炒股软件 编辑:程序博客网 时间:2024/06/03 13:39
大姐做了个生成MD5码的工具,用来做游戏资源的对比更新。让我给加个多线程。
百度到有两种方式在子线程中更新空间,如下
1、使用控件的 Invoke
比如我这里要添加内容到ListBox然后 更新
public void Notice(string notice) { Action<string> action = delegate(string str) { if (box != null) { box.Items.Add(notice); box.TopIndex = box.Items.Count - (int)(box.Height / box.ItemHeight); } }; box.Invoke(action, new object[] { notice }); }
2、使用SynchronizationContext的Post/Send方法更新
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication1{ public partial class Form1 : Form { SynchronizationContext m_context = null; Thread m_thread = null; public Form1() { InitializeComponent(); m_context = SynchronizationContext.Current; } private void button1_Click(object sender, EventArgs e) { m_thread = new Thread(new ThreadStart(LoopThread)); m_thread.Start(); } private void LoopThread() { m_context.Post(SetLabel, "test"); } private void SetLabel(object str) { label1.Text = str.ToString(); } private void label1_Click(object sender, EventArgs e) { } }}
然后 让 ListBox 自动定位到最后一个
box.TopIndex = box.Items.Count - (int)(box.Height / box.ItemHeight);
0 0
- Winform 子线程更新 控件
- C#、Winform子线程更新界面主线程信息
- C# winform 控件间线程更新问题处理方法
- C# Winform 跨线程更新UI控件常用方法汇总
- C# Winform 跨线程更新UI控件常用方法汇总
- C# Winform 跨线程更新UI控件常用方法汇总
- C# Winform 跨线程更新UI控件常用方法汇总
- C# Winform 跨线程更新UI控件常用方法汇总
- C# Winform 跨线程更新UI控件常用方法总结
- C# Winform 跨线程更新UI控件常用方法汇总
- 界面主线程,子线程更新主界面控件
- 在子线程中更新主窗体的控件
- C#子线程更新UI控件的方法总结
- C#子线程更新UI控件的方法实例总结
- Winform 线程 委托 更新 WinForm界面
- 子线程更新界面
- 子线程更新UI
- 子线程更新UI
- Scalaz(14)- Monad:函数组合-Kleisli to Reader
- Vector与ArrayList区别
- 设计模式 - 行为型模式 - 观察者模式
- php 不能通过表单获取所有select的值,用js赋值一个隐藏域
- 《剑指offer》——扑克牌顺子
- Winform 子线程更新 控件
- 有关fastjson输出的时候double不输出后面.0的问题
- Android使用XMPP协议、Openfire服务器和Smack类库实现即时通信
- FTGL用户指南 2.1.3~rc5
- GitHub添加SSH keys报错Key is invalid. It must begin with 'ssh-ed25519', 'ssh-rsa', 'ssh-dss', 'ecdsa-sha
- ERROR 2006 (HY000): MySQL server has gone away
- JAVA中String类的常见操作
- Xshell 安装教程
- poj3009Curling 2.0【dfs】