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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
Jina AI
Jina AI
博客园_首页
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
N
News | PayPal Newsroom
S
Security Archives - TechRepublic
量子位
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Cisco Blogs
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Scott Helme
Scott Helme
S
Securelist
Security Latest
Security Latest
爱范儿
爱范儿
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
L
LINUX DO - 最新话题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
月光博客
月光博客
T
Tailwind CSS Blog
Cloudbric
Cloudbric
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
D
DataBreaches.Net
博客园 - 【当耐特】
有赞技术团队
有赞技术团队

博客园 - IT 笔记

Subversion Revert File will not be shown in Modification Surprise vb.net WinForm don't require instance to access the instance value Properties.Settings show error 'System.Windows.Forms.PropertyStore' does not contain a definition for 'Settings' Add Image in MS Report RDLC by using Embedded Image 王爽汇编 Lab 13 Question 1, 用两个程序实现 Spring.NET Installation and setup the first program 王爽汇编语言 实验11 王爽汇编语言 检测点11.2 王爽 汇编语言程序课程设计1 王爽 汇编语言程序设计实验10,题三,数值转换。(Assembly experiment Convert HEX to Decimal and Show on screen ) 王爽 汇编语言程序设计 实验9 (Assembly Language Study) 王爽 汇编语言程序设计 检测点9.1 第2题 The NTVDM CPU has encountered an illegal instruction. CS:0006 IP:130a .... select files by multiple extension name Access Denied when running Spring.IocQuickStart.MovieFinder using vb.net read unix style text file How to install autotrace in fontforge My first postscript program .net 2 config
A generic winform skeleton support interactive UI during large job process
IT 笔记 · 2009-09-05 · via 博客园 - IT 笔记

It is quite annoying when a .net program try to update user interface control from a seperate long run task thread.

The runtime give me exception “Control control name accessed from a thread other than the thread it was created on.”

If it is in visual studio 2005, a lot of place recommend use "BackgroundWorker" to solve the problem.

But it is also not flexiable, It works fine with progressbar control only. If I need to display something on textbox, It is not straigt forward. For example, I want to change textbox background color.

I created my own skeleton code which can easily access any user interface control from seperate thread.

The key part is in UpdateUI_handler subroutine.

And if you want to access one control, you just need tell

  • what control you want to access,
  • what property of the control you want to access,
  • and what value you want to set.

That is very easy approach to solve the problem.

Below is my sample application snapshot

image

You can follow 3 steps below to create the project

  1. Create vb winform application in visual studio 2005.
  2. Drag a textbox, a label, a progressbar and a button on the form.
  3. Copy the code below and overwrite form1.vb
   1:  Imports System
   2:  Imports System.Threading
   3:  Imports system.reflection
   4:  Public Class Form1
   5:      Delegate Sub UpdateUI(ByVal UpdateParameter As UIUpdateParameter)
   6:      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   7:          Dim index As Int32
   8:          index = 10
   9:          Me.ProgressBar1.Maximum = index
  10:          Debug.WriteLine("Main Form thread: " & AppDomain.GetCurrentThreadId)
  11:          Dim thread As Thread
  12:          thread = New Thread(AddressOf DoTask)
  13:          thread.IsBackground = True
  14:          thread.Start()
  15:      End Sub 
  16:   
  17:      Private Sub UpdateUI_handler(ByVal UpdateParameter As UIUpdateParameter)
  18:          Debug.WriteLine("Update progress bar thread: " & AppDomain.GetCurrentThreadId)
  19:          Dim theControlType As Object = Nothing
  20:          Dim theControl As Control = Nothing
  21:          For Each p As System.Reflection.PropertyInfo In Me.Controls(UpdateParameter.controlName).GetType().GetProperties()
  22:              theControl = Me.Controls(UpdateParameter.controlName)
  23:              theControlType = Convert.ChangeType(theControl, theControl.GetType)
  24:              If Not IsNothing(p.GetValue(theControlType, Nothing)) Then
  25:                  If UCase(p.Name.ToString) = UCase(UpdateParameter.PropertyName) Then
  26:                      p.SetValue(theControlType, Convert.ChangeType(UpdateParameter.value, p.PropertyType), Nothing)
  27:                  End If
  28:              End If
  29:          Next
  30:      End Sub
  31:      Private Sub DoTask(ByVal t)
  32:          For i As Integer = 1 To 10
  33:              Thread.Sleep(500)
  34:              Debug.WriteLine("Do Task thread: " & AppDomain.GetCurrentThreadId) 
  35:   
  36:              Dim uiUpdate2 As UpdateUI
  37:              uiUpdate2 = New UpdateUI(AddressOf UpdateUI_handler) 
  38:   
  39:              Dim UpdateParameter As New UIUpdateParameter
  40:              UpdateParameter.controlName = "ProgressBar1"
  41:              UpdateParameter.PropertyName = "value"
  42:              UpdateParameter.value = i
  43:              Me.Invoke(uiUpdate2, UpdateParameter) 
  44:   
  45:              UpdateParameter.controlName = "Label1"
  46:              UpdateParameter.PropertyName = "Text"
  47:              UpdateParameter.value = i
  48:              Me.Invoke(uiUpdate2, UpdateParameter) 
  49:   
  50:              UpdateParameter.controlName = "Textbox1"
  51:              UpdateParameter.PropertyName = "backcolor"
  52:              If i Mod 2 = 0 Then
  53:                  UpdateParameter.value = Color.Blue
  54:              Else
  55:                  UpdateParameter.value = Color.Yellow
  56:              End If 
  57:   
  58:              Me.Invoke(uiUpdate2, UpdateParameter)
  59:          Next i
  60:      End Sub
  61:  End Class 
  62:   
  63:  Public Class UIUpdateParameter
  64:      Public controlName As String
  65:      Public PropertyName As String
  66:      Public value As Object
  67:  End Class