PeekMessage, DispatchMessage in C# ??

  • Thread starter Thread starter Jozsef Bekes
  • Start date Start date
J

Jozsef Bekes

Hi All,

I am trying to implement a general threading approach in C#, and I have
faced this problem:
I have the function f_main running in thread th1. I want to invoke two
functions f1 and f2 in th_2. From f_main I want to do two distinct function
calls, the first results only in invoking f1 in the context of th_2, the
second function call would invoke only f2 in the context of th_2. f1 and f2
must run in the same thread context.
My approach in C++ would be to start a thread, put the usual while loop with
PeekMessage and DispatchMessage and trigger the function invokations via
PostThreadMessage. I cannot find such an opportunity in C# except for using
the windows API and implementing the stuff as if I was using C++ (that is I
guess not a good approach). Could someone please give me a suggestion how I
should proceed?

Thank you for your help.

Best Regards,
Jozsi
 
Hi Alex,

thank you for your answer. My problem with this approach is that it does not
ensure that f1 and f2 run in the same thread. There are two approaches in
the article, one is said to use ThreadPool, the other is said to use Thread.
Since I have found no hints about how to determine which thread the
ThreadPool should select for the desired execution, I guess using ThreadPool
I cannot ensure that f1 and f2 run in the same thread. Moreover, the simple
Thread approach also uses ThreadPool:

MSDN: "If the BeginInvoke method is called, the common language runtime will
queue the request and return immediately to the caller. The target method
will be called on a thread from the thread pool."

Regards,
Jozsi
 
Back
Top