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

PostMessage unsafe?

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 everything works just fine. Still I feel somewhat
uncomfortable.

Sergei
Aug 31 '06 #1
10 2544
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****************@TK2MSFTNGP02.phx.gbl...
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 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****************@TK2MSFTNGP02.phx.gbl...
| 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 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****************@TK2MSFTNGP02.phx.gbl...
| 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 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****@summertime.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****@summertime.mtu-net.ruwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
|
| Willy Denoyette [MVP] wrote:
| "Sergei" <se****@nospam.summertime.mtu-net.ruwrote in message
| news:%2****************@TK2MSFTNGP02.phx.gbl...
| | 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 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.Handle,...);

this will throw an exception when executed on another thread, but it's not
PostMessage who throws, it's the property getter MyControl.Handle. who
throws CheckForIllegalCrossThreadCalls.
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****@summertime.mtu-net.ruwrote in message
news:11**********************@74g2000cwt.googlegro ups.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 CheckForIllegalCrossThreadCalls
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****@summertime.mtu-net.ruwrote in message
news:11**********************@d34g2000cwd.googlegr oups.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 CheckForIllegalCrossThreadCalls
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
Willy Denoyette [MVP] wrote:
<se****@summertime.mtu-net.ruwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
|
| Willy Denoyette [MVP] wrote:
| "Sergei" <se****@nospam.summertime.mtu-net.ruwrote in message
| news:%2****************@TK2MSFTNGP02.phx.gbl...
| | 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 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.Handle,...);

this will throw an exception when executed on another thread, but it's not
PostMessage who throws, it's the property getter MyControl.Handle. who
throws CheckForIllegalCrossThreadCalls.
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.
I can't reproduce this today because I changed the job and in the
new place only have VS.2003 but I remember I tried this converting
the Forms handle into int, and than in the worker thread converting
it back into IntPtr. It didn't work.

Anyway thanks for responding
Sergei

Sep 5 '06 #11

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...
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. ...
11
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...
3
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...
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...
21
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.