473,659 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PostMessage unsafe?

Can anyone explain why PostMessage to a Windows Form
is considered unsafe and raises InvalidOperatio nException?
I can get rid of that setting CheckForIllegal CrossThreadCall s
to false and everything works just fine. Still I feel somewhat
uncomfortable.

Sergei
Aug 31 '06 #1
10 2576
Sergei,

What is this method? AFAIK Widnows Forms doesn't provide such a method. Are
you talking about the API method that can be used through PInvoke? If you
are then this is the answer of your question why it is unsafe.

Brief cosulting with the MSDN tells me that there is PostMessage for
WindowsCE. Is it the one that you are asking for?
--

Stoitcho Goutsev (100)

"Sergei" <se****@nospam. summertime.mtu-net.ruwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Can anyone explain why PostMessage to a Windows Form
is considered unsafe and raises InvalidOperatio nException?
I can get rid of that setting CheckForIllegal CrossThreadCall s
to false and everything works just fine. Still I feel somewhat
uncomfortable.

Sergei

Aug 31 '06 #2

"Sergei" <se****@nospam. summertime.mtu-net.ruwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
| Can anyone explain why PostMessage to a Windows Form
| is considered unsafe and raises InvalidOperatio nException?
| I can get rid of that setting CheckForIllegal CrossThreadCall s
| to false and everything works just fine. Still I feel somewhat
| uncomfortable.
|
| Sergei

IMO this is not caused by the PostMessage API call, but what I know is that
you are somehow "touching" the UI from a thread other than the UI thread.
This is something you should not do, when you need to access UI elements
from another thread, you have to marshal the call's to the control's owning
thread using one of the Control.Invoke or BeginInvoke methods.

Willy.

Aug 31 '06 #3
Stoitcho Goutsev (100) wrote:
Sergei,

What is this method? AFAIK Widnows Forms doesn't provide such a method. Are
you talking about the API method that can be used through PInvoke? If you
are then this is the answer of your question why it is unsafe.

Brief cosulting with the MSDN tells me that there is PostMessage for
WindowsCE. Is it the one that you are asking for?
No, it's API and I call it from another the UI thread.
Still I don't understand why it is thread unsafe.
I've always used PostMessage for safe interthread communication, now it
appears it is not that safe.

Sergei

Sep 4 '06 #4

Willy Denoyette [MVP] wrote:
"Sergei" <se****@nospam. summertime.mtu-net.ruwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
| Can anyone explain why PostMessage to a Windows Form
| is considered unsafe and raises InvalidOperatio nException?
| I can get rid of that setting CheckForIllegal CrossThreadCall s
| to false and everything works just fine. Still I feel somewhat
| uncomfortable.
|
| Sergei

IMO this is not caused by the PostMessage API call, but what I know is that
you are somehow "touching" the UI from a thread other than the UI thread.
Yes, I call it from another than UI thread.
This is something you should not do, when you need to access UI elements
from another thread, you have to marshal the call's to the control's owning
thread using one of the Control.Invoke or BeginInvoke methods.
I do not access any elements, I just post WM_USER message to a windows
handle.
And I still can't understand what might be unsafe in doing that.

Sergei

Sep 4 '06 #5
On 4 Sep 2006 05:36:46 -0700, se****@summerti me.mtu-net.ru wrote:
>Stoitcho Goutsev (100) wrote:
>Sergei,

What is this method? AFAIK Widnows Forms doesn't provide such a method. Are
you talking about the API method that can be used through PInvoke? If you
are then this is the answer of your question why it is unsafe.

Brief cosulting with the MSDN tells me that there is PostMessage for
WindowsCE. Is it the one that you are asking for?

No, it's API and I call it from another the UI thread.
Still I don't understand why it is thread unsafe.
I've always used PostMessage for safe interthread communication, now it
appears it is not that safe.
I think it is not really so much "unsafe" as "unmanaged" , which is not
the same at all. That being said, if you post a message to another
application, you have no idea what is going to happen, so from that
point of view it is definitely "unsafe".

--
Posted via a free Usenet account from http://www.teranews.com

Sep 4 '06 #6

<se****@summert ime.mtu-net.ruwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
|
| Willy Denoyette [MVP] wrote:
| "Sergei" <se****@nospam. summertime.mtu-net.ruwrote in message
| news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
| | Can anyone explain why PostMessage to a Windows Form
| | is considered unsafe and raises InvalidOperatio nException?
| | I can get rid of that setting CheckForIllegal CrossThreadCall s
| | to false and everything works just fine. Still I feel somewhat
| | uncomfortable.
| |
| | Sergei
| >
| IMO this is not caused by the PostMessage API call, but what I know is
that
| you are somehow "touching" the UI from a thread other than the UI
thread.
|
| Yes, I call it from another than UI thread.
|
| This is something you should not do, when you need to access UI elements
| from another thread, you have to marshal the call's to the control's
owning
| thread using one of the Control.Invoke or BeginInvoke methods.
|
| I do not access any elements, I just post WM_USER message to a windows
| handle.
| And I still can't understand what might be unsafe in doing that.

This exception is not thrown by the PostMessage call (it simply can't), IMO
somehow, you are getting the handle to be used in your PostMessage call on
the same (non UI) thread and this is when you get the exception, this handle
was created on the UI thread and the frameword guards against cross-thread
accesses to HWND's.
Say you have a Control instance MyControl on the UI, and you have written:

PostMessage (MyControl.Hand le,...);

this will throw an exception when executed on another thread, but it's not
PostMessage who throws, it's the property getter MyControl.Handl e. who
throws CheckForIllegal CrossThreadCall s.
Now, in this case it's not an error to use the handle, but it's better to
correct the code such that you are calling the API on the UI thread. Another
option is to cache the handle on the UI thread and read the IntPtr (wrapping
the handle) from other threads.

Willy.
Sep 4 '06 #7
Oh, you are say that you get some threading error. You can call PostMessage
from any thread. It is not thread dependent. I believe your problem is
somewhere else.

Can you post some compilable sample code? I don't think the problem is
thread related. However calling unmanaged code (API) is unsafe operation and
can be executed only when the application is running with full trust.
--
HTH
Stoitcho Goutsev (100)

<se****@summert ime.mtu-net.ruwrote in message
news:11******** **************@ 74g2000cwt.goog legroups.com...
Stoitcho Goutsev (100) wrote:
>Sergei,

What is this method? AFAIK Widnows Forms doesn't provide such a method.
Are
you talking about the API method that can be used through PInvoke? If you
are then this is the answer of your question why it is unsafe.

Brief cosulting with the MSDN tells me that there is PostMessage for
WindowsCE. Is it the one that you are asking for?

No, it's API and I call it from another the UI thread.
Still I don't understand why it is thread unsafe.
I've always used PostMessage for safe interthread communication, now it
appears it is not that safe.

Sergei

Sep 5 '06 #8
Stoitcho Goutsev (100) wrote:
Oh, you are say that you get some threading error. You can call PostMessage
from any thread. It is not thread dependent. I believe your problem is
somewhere else.

Can you post some compilable sample code? I don't think the problem is
thread related. However calling unmanaged code (API) is unsafe operation and
can be executed only when the application is running with full trust.
Alas. I left that job on 31.08 and it's the second day in another
company. Moreover here I work with VS.2003 while there it
was 2005. And the error is VS version specific. On 2005 certain
operations on WinForms raise CheckForIllegal CrossThreadCall s
exceptions if performed on another than the form's thread.
I couldn't understand why a PostThread API call would raise the
exception.

Anyway thanks for responding
Sergei

Sep 5 '06 #9
Sergei,

It seems you found a solution to your problem :-P
Good luck with your new job.

--
Stoitcho Goutsev (100)
"Sergei" <se****@summert ime.mtu-net.ruwrote in message
news:11******** **************@ d34g2000cwd.goo glegroups.com.. .
Stoitcho Goutsev (100) wrote:
>Oh, you are say that you get some threading error. You can call
PostMessage
from any thread. It is not thread dependent. I believe your problem is
somewhere else.

Can you post some compilable sample code? I don't think the problem is
thread related. However calling unmanaged code (API) is unsafe operation
and
can be executed only when the application is running with full trust.

Alas. I left that job on 31.08 and it's the second day in another
company. Moreover here I work with VS.2003 while there it
was 2005. And the error is VS version specific. On 2005 certain
operations on WinForms raise CheckForIllegal CrossThreadCall s
exceptions if performed on another than the form's thread.
I couldn't understand why a PostThread API call would raise the
exception.

Anyway thanks for responding
Sergei

Sep 5 '06 #10

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

Similar topics

2
1863
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 below Private Declare Auto Function PostMessage Lib "user32" ( _ ByVal hWnd As IntPtr, _ ByVal Msg As Integer, _ ByVal wParam As Long, _
15
8443
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 System.Windows.Forms; using System.Runtime.InteropServices; namespace mynamespace
12
7187
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 illegal cross thread operation if posting a message. Is this a bug introduced in latest version of dotnet ? It is very legal to use postmessage because the message will execute in context of the thread the one was posted to. Same with...
2
2615
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 message msg is now in the wparam position and msg is set to 0 (NULL message). Does anyone know why this behaviour is occurring ? I've included a simple test form below to illustrate this...
3
5084
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. I've got a C# class library project with two classes: Class 1 is derives from : System.Windows.Forms.Form and overrides the base WndProc method for the purpose of receiving and handeling user defined messages:
11
9162
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different approaches, and both fail. I don't know what to try next. +++++++++++++++++++++++++++++++++++++++++++++++++++++ APPROACH #1: (this approach makes the most sence to me) Configure the first parameter of the thread's PostMessage() to talk to the...
3
5624
by: easoftware | last post by:
In my ComboBox, I am trying to post the CBN_EDITCHANGE message after the CBN_SELCHANGE is received, but I am having no luck. I can trap for the CBN_SELCHANGE message, but I cannot post the CBN_EDITCHANGE message. I think my PostMessage parameters are wrong, but I cannot find what they should be. Any help would be greatly appreciated. Thanks. Here is my code.
3
9460
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 Button1.Click AppGlobals.SetHandles(Me.Handle.ToInt64()) Call OpenModel() End Sub
21
6877
by: one2001boy | last post by:
PostMessage() function returns ERROR_NOT_ENOUGH_QUOTA after running in a loop for 700 times, but the disk space and memory are still big enough. any suggestion to resolve this problem? thanks.
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.