473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PeekMessage, DispatchMessage in C# ??

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
Nov 17 '05 #1
2 7830
Have you thought about the following:

Create an object X that performs the work required by T1 or T2 (in a
non-threaded manner, i.e. forget about threads for the time being while you
design X).

Then make X a static member of your main class.

Launch T1 and T2.

In T1 when you have a method call into X

Enter Critical Section
Perform work with X
Leave Critical Section

In T2 when you have a method call into X

Enter Critical Section
Perform work with X
Leave Critical Section

This will take care of any concurrency issues you might have. But to
address the main problem is when do you signal T2 to perform some work in T2
context, well you can create an object Y which will hold a queue of requests
for T2. Lets say that object Y has to boolean variables for example:

doWorkInMethod1 = false;
doWorkInMethod2 = true;

You will declare object Y as static on your main and then use the same
approach with critical section, except that in T2 will periodically check
object Y and "if doWorkInMethod1 == true then call method1 in current
context". T1 will periodically set the values of object Y to signal what
work must be done in T2. As long as T1 and T2 are not into Y at the same
time (thread concurrency control) you should be ok.

Does it make any sense? I suppose that would depend on wether I understood
your problem correctly.

Alex

When T1 needs work performed by T2, T1
"Jozsef Bekes" <bj****@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
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

"Alex Passos" <bz@netmerlin.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Take a look at this article, it might help you get started:

http://www.codeproject.com/csharp/winformthreading.asp

Alex

"Jozsef Bekes" <bj****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #2
Hi Alex,

thank you for your answer. Actually, I have already implemented the concept
in a different way, but it seems strange to me that this is so complicated
in C#, the framework gave much better support in VC6 for this problem. What
I did is the following:
I have a queue q. I enter whatever I want in th1 into the q. I have a
working function running in th2 that has a loop, that checks the content of
q, if there is something to process, I process it, otherwise suspend the
thread. When I enter something to q from th1, I check the state of th2, if
it is suspended, I resume it. Of course there is cleanup code and variable
multithread access protection for q. Actually I had to rewrite the whole
messaging concept in C# - not complicated, but seems pure rework.
I also noticed yesterday that controls do not have message queues either, it
is a simple callback mechanism that is described as messaging. Well, I have
been into C# only since Monday, so I might face other surprises as well :-))

Thanks for your help,
Jozsi

"Alex Passos" <bz@netmerlin.nospam.com> wrote in message
news:Ow**************@TK2MSFTNGP15.phx.gbl...
Have you thought about the following:

Create an object X that performs the work required by T1 or T2 (in a
non-threaded manner, i.e. forget about threads for the time being while
you design X).

Then make X a static member of your main class.

Launch T1 and T2.

In T1 when you have a method call into X

Enter Critical Section
Perform work with X
Leave Critical Section

In T2 when you have a method call into X

Enter Critical Section
Perform work with X
Leave Critical Section

This will take care of any concurrency issues you might have. But to
address the main problem is when do you signal T2 to perform some work in
T2 context, well you can create an object Y which will hold a queue of
requests for T2. Lets say that object Y has to boolean variables for
example:

doWorkInMethod1 = false;
doWorkInMethod2 = true;

You will declare object Y as static on your main and then use the same
approach with critical section, except that in T2 will periodically check
object Y and "if doWorkInMethod1 == true then call method1 in current
context". T1 will periodically set the values of object Y to signal what
work must be done in T2. As long as T1 and T2 are not into Y at the same
time (thread concurrency control) you should be ok.

Does it make any sense? I suppose that would depend on wether I understood
your problem correctly.

Alex

When T1 needs work performed by T2, T1
"Jozsef Bekes" <bj****@hotmail.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
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

"Alex Passos" <bz@netmerlin.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Take a look at this article, it might help you get started:

http://www.codeproject.com/csharp/winformthreading.asp

Alex

"Jozsef Bekes" <bj****@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michiel Overtoom | last post by:
Hi, Is it possible to write a standalone Win32 application using PHP? Like creating a window from the PHP script, putting buttons and textboxes on it, handling clicks on the buttons etc..., ...
2
by: Phoebe | last post by:
Hello, Can anyone tell when we need to use the function IsDialogMessage? What is the different between Code 1 and Code 2? Code 1: --------- while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {...
2
by: JR | last post by:
Hi folks, How can I implement the following in C#? while(AppExitFlag == false) { if(AppPausedFlag == true) { GetMessage(); // Process messages.
2
by: Jeff | last post by:
What is the .Net Framework equivalent of the Windows API function PeekMessage? Or any other method that will do the same thing. I simply need to check to see if there are messages waiting to be...
3
by: John | last post by:
I get the following VB6 sample and it doesn't work in VB .Net. Can anyone help to make it work in VB .Net? Structure POINTAPI Dim x As Long Dim y As Long End Structure Structure MSG Dim...
2
by: blueturtle | last post by:
Hi, I'm newbie to C#, and I would like to know what is the common solution to a problem that I encounter. The scenario: Performing a long task, without blocking the UI thread, so it will stay...
1
by: Bruce | last post by:
I worte an OCX with events. In order to allow the dialog box (or whatever) to be updated by the event, I had to add a messagepump to the event. void MessagePump() { MSG msg; while...
1
by: SeeSharp Bint | last post by:
Hi I'm using some of the definitions I found pinvoke.net for PostMessage and PeekMessage as follows public static extern bool PostMessage( HandleRef hWnd,// handle to destination window...
28
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.