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

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

博客园 - 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 ...
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