473,320 Members | 1,922 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,320 software developers and data experts.

Marshalling (ie PostMessage) in C#.

I have a component (with NO user interface) in C# that
fires events from a seperate thread. The client of the C#
component does processing with UI elements and other non
thread safe variables within these events. I do NOT want
the client to be processing the event from the secondary
thread created in the component. I want it to process from
the Main UI thread.

I have scanned through MSND documentation and it keeps
saying to use control.BeginInvoke() in the client to do
this. However requiring the client of my component to do
this is one more thing that can go wrong for users of my
component. So I want it performed from within the
component. In C++ I could acheive this by using PostMessage
() specifying an HWND in the main thread.
How do I do this in C# from within the component that is
invisible at runtime. My component object does not have a
BeginInvoke on it. Should I create a hidden control from
within the component simply to marshall back to main
thread from within itself?

Any Ideas would be appreciated. TIA
-Greg.

Nov 15 '05 #1
2 5219
Gregory,

What you want to do is expose some sort of method or property that will
take an implementation of the ISynchronizeInvoke interface. This interface
is used to synchronize the events that one would want to fire on an object.
For windows controls, it does exactly what you would do, posting a message
to the UI thread for a control to handle.

However, this isn't the only way to synchronize events, so the interface
is used to abstract that. What your component should do is take an instance
of this interface and then make the calls to Invoke on that interface. All
you have to say is where the affinity lies by passing in the appropriate
implementation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Gregory Goeppel" <gr**********@ser.com> wrote in message
news:00****************************@phx.gbl...
I have a component (with NO user interface) in C# that
fires events from a seperate thread. The client of the C#
component does processing with UI elements and other non
thread safe variables within these events. I do NOT want
the client to be processing the event from the secondary
thread created in the component. I want it to process from
the Main UI thread.

I have scanned through MSND documentation and it keeps
saying to use control.BeginInvoke() in the client to do
this. However requiring the client of my component to do
this is one more thing that can go wrong for users of my
component. So I want it performed from within the
component. In C++ I could acheive this by using PostMessage
() specifying an HWND in the main thread.
How do I do this in C# from within the component that is
invisible at runtime. My component object does not have a
BeginInvoke on it. Should I create a hidden control from
within the component simply to marshall back to main
thread from within itself?

Any Ideas would be appreciated. TIA
-Greg.

Nov 15 '05 #2
Thanks,

Here's what I did and it works great! The control object
provides an implementation of ISynchronizeInvoke. So I
instance it in the components main thread. So what ever
thread in the client creates the component that thread
should get affinity.

private Control m_ControlInMainThread;

private void startPopupAlertThread()
{
//We are in Components Main Thread Now

m_ControlInMainThread = new Control();
m_ControlInMainThread.CreateControl();
ThreadStart entryPoint = new ThreadStart
(GetPopUpAlertsThread);
Thread popupAlertThread = new Thread(entryPoint);
popupAlertThread.Start();
}

Now from the Alert Thread I can do the following

//Post this Event back to Main Thread!
m_ControlInMainThread.BeginInvoke(m_WebCtrlPopupHa ndler,
new object[] {alert});

Now My Thread ID's show that the Handler in the client
matches the Main UI thread in which I instance the
component from the client.

If there are any reasons you can think of that I shouldn't
be doing things this way, let me know. So far in my
testing it works great.

Thanks Again,
Greg.

-----Original Message-----
Gregory,

What you want to do is expose some sort of method or property that willtake an implementation of the ISynchronizeInvoke interface. This interfaceis used to synchronize the events that one would want to fire on an object.For windows controls, it does exactly what you would do, posting a messageto the UI thread for a control to handle.

However, this isn't the only way to synchronize events, so the interfaceis used to abstract that. What your component should do is take an instanceof this interface and then make the calls to Invoke on that interface. Allyou have to say is where the affinity lies by passing in the appropriateimplementation.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Gregory Goeppel" <gr**********@ser.com> wrote in message
news:00****************************@phx.gbl...
I have a component (with NO user interface) in C# that
fires events from a seperate thread. The client of the C# component does processing with UI elements and other non
thread safe variables within these events. I do NOT want
the client to be processing the event from the secondary
thread created in the component. I want it to process from the Main UI thread.

I have scanned through MSND documentation and it keeps
saying to use control.BeginInvoke() in the client to do
this. However requiring the client of my component to do
this is one more thing that can go wrong for users of my
component. So I want it performed from within the
component. In C++ I could acheive this by using PostMessage () specifying an HWND in the main thread.
How do I do this in C# from within the component that is
invisible at runtime. My component object does not have a BeginInvoke on it. Should I create a hidden control from
within the component simply to marshall back to main
thread from within itself?

Any Ideas would be appreciated. TIA
-Greg.

.

Nov 15 '05 #3

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

Similar topics

2
by: Paul | last post by:
Hi all. Probably a quick one, I'm using PostMessage to send keys to an applicatoin but how do I convert the key string to a long values so that I can pass this to the wParam. The definition is...
15
by: James | last post by:
In my code I have a problem in abtaining a windows handle. I do not know the namespace to obtain the windows hanle. here is the code. using System; using System.Collections; using...
12
by: Wilfried Mestdagh | last post by:
Hi, Using P/Invoke I use PostMessage to send a message from one thread to another. I'm pretty sure this has already worked in dotnet. but I've upgraded version 2 few day ago. Now I have an...
6
by: SM | last post by:
Hello group, i'm converting a VB6 application for .NET Framework. The application depends on a DLL written in standard C. The C DLL code creates an invisible window with CreateWindow() with the...
2
by: Lenster | last post by:
When using PostMessage to post myself a message, the msg and wparam parameters somehow get swapped over. They are in the correct order when calling PostMessage but by the time wndproc handles the...
3
by: Max M. Power | last post by:
When I use the SendMessage API I can sucessfully send and receive a user defined message. When I use the PostMessage API I can NOT sucessfully send and receive the same user defined message. ...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
3
by: knikkix | last post by:
Hi, I created a form in VB.Net with only one button. This is the code in button click event Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
10
by: Sergei | last post by:
Can anyone explain why PostMessage to a Windows Form is considered unsafe and raises InvalidOperationException? I can get rid of that setting CheckForIllegalCrossThreadCalls to false and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.