---
title: "update the GUI from another thread in C#"  
description: "update the GUI from another thread in C#"  
author: "Anonymous User"  
published: 2014-10-09  
updated: 2014-10-09  
canonical: https://www.mindstick.com/forum/2349/update-the-gui-from-another-thread-in-c-sharp  
category: "c#"  
tags: ["asp.net", "c#"]  
reading_time: 2 minutes  

---

# update the GUI from another thread in C#

What is the simplest way to [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) a [Label](https://www.mindstick.com/articles/12835/print-label-tracking-how-do-these-features-make-auspost-woocommerce-plugin-better) from another thread?\
I have a [Form](https://www.mindstick.com/forum/6/multi-form-mfc-application) on thread1, from that I'm [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) another thread (thread2). While thread2 is [processing](https://www.mindstick.com/blog/254/background-processing-in-android) some [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) I would like to update a Label on the Form with the [current status](https://answers.mindstick.com/qa/100658/what-s-the-current-status-of-the-syrian-conflict) of thread2's work.\
How can I do that?

## Replies

### Reply by Anonymous User

For .NET 3.0 you should use this code.

```
private delegate void SetPropertyThreadSafeDelegate<TResult>(Control @this, Expression<Func<TResult>> property, TResult value);         public static void SetPropertyThreadSafe<TResult>(this Control @this, Expression<Func<TResult>> property, TResult value)        {            var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;             if (propertyInfo == null ||                !@this.GetType().IsSubclassOf(propertyInfo.ReflectedType) ||                @this.GetType().GetProperty(propertyInfo.Name, propertyInfo.PropertyType) == null)            {                throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control.");            }             if (@this.InvokeRequired)            {                @this.Invoke(new SetPropertyThreadSafeDelegate<TResult>(SetPropertyThreadSafe), new object[] { @this, property, value });            }            else            {                @this.GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null, @this, new object[] { value });            }        }
```

which uses LINQ and lambda expressions to allow much cleaner, simpler and safer syntax:\

myLabel.SetPropertyThreadSafe(() => myLabel.Text, [status](https://www.mindstick.com/articles/157184/check-lic-policy-status-online-by-policy-number));\

Not only is the property name now checked at compile time, the property's type is as well, so it's impossible to (for example) assign a string value to a boolean property, and hence cause a runtime exception.

Unfortunately this doesn't stop anyone from doing stupid things such as passing in another Control's property and value, so the following will happily compile:

myLabel.SetPropertyThreadSafe(() => aForm.ShowIcon, false);\

Hence I added the runtime checks to ensure that the passed-in property does actually belong to the Control that the method's being called on. Not perfect, but still a lot better than the .NET 2.0 version.\


---

Original Source: https://www.mindstick.com/forum/2349/update-the-gui-from-another-thread-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
