惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 咒语

Azure OAuth2 PostMan 授权代码 每当双11来时,商家与京东的那些骚操作,京东30天保价不能信 Microsoft.Extensions.DependencyInjection 阅读笔记 EasyToLearnDesignPattern C#中关于表达式与委托在EF中的不同表现总结 软件开发中的版本号 The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. ABP项目依赖图,根据自已生在的Demo项目分析而得 学习ELk之----02. Elastic Search操作入门 学习ELK之----01.建立ELK测试项目 一目了然呀的VS2017 Live Test 在Hyper-V上安装RemixOS 的Android模拟器 配置Asp.Net Web项目NLog配置文件的位置 配置WinRM的Https 测试EntityFramework,Z.EntityFramework.Extensions,原生语句在不同的查询中的表现。原来池化与非池化设定是有巨大的影响的。 消费RabbitMQ时的注意事项,如何禁止大量的消息涌到Consumer RabbitMQ调试与测试工具-v1.0.1 -提供下载测试与使用 安装TFS2015后启用生成功能 log4net在Realse下有个好大的坑呀。
跨线程 操作Winform 主UI时的扩展方法
咒语 · 2021-12-16 · via 博客园 - 咒语

2021-12-16 13:32  咒语  阅读(128)  评论()    收藏  举报

我们在写WinForm程序的时候会发现,你在非UI线程里的的更改UI里的对象时会抛出异常。

这个时候就会要求使用控件的跨线程判断与操作,一个一个的元素的去写太麻烦了。我写了个简单的扩展。

然后,就像只有一个线程一样的去操作吧。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace System.Windows.Forms
{
    public static class FormExtensions
    {


        public static void Push(this TextBox box, string message, TraceLevel level = TraceLevel.Info)
        {
            box.SafeChange(() =>
            {
                if (box.Lines.Length > 5000) { box.Clear(); }
                box.AppendText($"[{DateTime.Now:HH:mm:ss fff}] [{level}] {message} {Environment.NewLine}");
            });
        }


        public static void Begin(this Button button)
        {
            button.SafeChange(() =>
            {
                button.Tag = button.Text;
                button.Text = $"{button.Text} ...";
                button.Enabled = false;
            });
        }

        public static void End(this Button button)
        {
            button.SafeChange(() =>
            {
                button.Text = (string)button.Tag;
                button.Enabled = true;
            });
        }



        #region Contrl Thread Safe operation.

        public static void SafeChange(this Control control, Action action)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(_safeChangeAction, control, action);
            }
            else
            {
                _safeChangeAction(control, action);
            }
        }

        private static Action<Control, Action> _safeChangeAction = SafeChangeForAction;

        private static void SafeChangeForAction(Control button, Action action)
        {
            action?.Invoke();
        }

        #endregion

    }
}

在主线程里操作时直接调用即可。 

 txtMessage.Push(msg);

或者你再包装成一个方法。