Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a UI thread in C#

I know I can start a new worker thread from with .NET. But how do I start a new UI thread (like in MFC)?

I don't mind if the solution is restricted to Windows boxes only; I also would like the solution to be purely .NET - no p/invokes to CreateThread, etc.

Any input appreciated.

like image 515
Filip Frącz Avatar asked Apr 13 '09 19:04

Filip Frącz


People also ask

How do I start a thread in C#?

Steps to create a thread in a C# Program: First of all import System. Threading namespace, it plays an important role in creating a thread in your program as you have no need to write the fully qualified name of class everytime. Now, create and initialize the thread object in your main method.

What is a main thread in C?

A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called an thread, and each thread defines a separate path of execution. Main Thread. When a C# program starts up, one thread begins running immediately. This is usually called the main thread of our program.

Does C support multi threading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

Can we update UI from thread?

Worker threads However, note that you cannot update the UI from any thread other than the UI thread or the "main" thread. To fix this problem, Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help: Activity.


2 Answers

Use Application.Run - it starts a message loop in the current thread. There are overloads to take a form to start with, or an application context, or neither. (If you want to do this for a new thread, you need to create the thread and start it in the normal way, and make it call Application.Run.)

like image 192
Jon Skeet Avatar answered Sep 18 '22 13:09

Jon Skeet


If you are interested in using WPF, check out MSDN's article WPF's Threading Model, in particular the "Multiple Windows, Multiple Threads" section. It details what you need to do to create a new Dispatcher and show a window on a new thread.

like image 43
Erich Mirabal Avatar answered Sep 19 '22 13:09

Erich Mirabal