Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# what is the recommended way of passing data between 2 threads?

I have my main GUI thread, and a second thread running inside it's own ApplicationContext (to keep it alive, even when there is no work to be done). I want to call a method on my 2nd thread from my GUI thread, but if I just call thread.Method(); it seems to be running on my main GUI thread and causes my GUI to become unresponsive. What is the best way to call methods on different threads?

Update: What I'm really looking to do here is communicate between 2 threads, not communicate with a GUI. The GUI just happens to be one of the threads that will need to communicate with my 2nd thread.

Update #2: Ok, I must really be missing something. I created an event and a delegate and had my worker thread subscribe to the event. But when I call Invoke(MyEvent); from my GUI thread the work that the worker thread does ends up being on the GUI thread and hangs the GUI thread until it's done processing. Is what I'm trying to do even possible, without polling on a static object?

like image 971
Jon Tackabury Avatar asked Dec 02 '08 17:12

Jon Tackabury


People also ask

What does || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is ?: operator in C?

In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical functions. It is a symbol that operates on a value or a variable. For example, + and - are the operators to perform addition and subtraction in any C program.

Is an operator in C?

Summary. An operator is a symbol which operates on a variable or value. There are types of operators like arithmetic, logical, conditional, relational, bitwise, assignment operators etc. Some special types of operators are also present in C like sizeof(), Pointer operator, Reference operator etc.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


1 Answers

Wow, I can't believe how may people didn't bother reading the question.

Anyways, this is what I do.

  1. Create you "message" classes. This stores all the information you want to share.
  2. Create a Queue<T> for each thread. Use a SyncLock (C# lock) to read/write to it.
  3. When you want to talk to a thread, send it a message object with a copy of all the information it needs by adding the message to the queue.
  4. The worker thread can then read from the queue, reading and processing each message in order. When there are no messages, simply sleep.

Make sure that you don't share objects between the two threads. Once your GUI thread sticks a message in the Queue, the GUI thread no longer owns the message. It cannot hold a reference to the message, or you will get yourself into trouble.

This won't give you the best possible performance, but it will be good enough for most applications. And more importantly, it will make it much harder to make a mistake.

UPDATE: Don't use a SyncLock and Queue. Instead use a ConcurrentQueue, which will handle any locking automatically for you. You'll get better performance and are less likely to make a mistake.

like image 118
Jonathan Allen Avatar answered Oct 11 '22 12:10

Jonathan Allen