473,837 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

illegal cross thread operation by PostMessage

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 SendMessage.

Can someone enlighten what this is about ?

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 17 '05
12 7204
Hi Willy,
What you could do to bypass the check is store the Handle in class variable,
and use that value when calling PostMessage.


Thank you. I tryed and it works as a glance. Thx :)

rgds, Wilfried
Nov 17 '05 #11
"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.com> wrote in
message news:38******** *************** ***********@mic rosoft.com...
const int WM_UPDATE = 0x401;
public void WMUpdate()
{
Win32.PostMessa ge(Handle, WM_UPDATE, 0, 0);
}


I'm using MS C# .Net, and can't find the "Win32.PostMess age" function.
I found a "Microsoft.Win3 2" namespace, but no PostMessage function.
Where is this function buried?

Thanks
DanB
Nov 17 '05 #12
you have to make them yourself. I have most importand of them in a separate
class, here is importand part:

#region Using directives

using System;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows. Forms;
using System.Runtime. InteropServices ; // needed to call external
application (winAPI dll)
#endregion

namespace Mestdagh.biz
{
public class Win32
{
[DllImport("kern el32.dll")]
public static extern Boolean AllocConsole();
[DllImport("User 32.dll")]
public static extern IntPtr FindWindow(stri ng ClassName, string
WindowName);
[DllImport("User 32.dll")]
public static extern Boolean PostMessage(Int Ptr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
[DllImport("User 32.dll")]
public static extern Boolean SendMessage(Int Ptr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
[DllImport("User 32.dll")]
public static extern Int32 SetForegroundWi ndow(int hWnd);
[DllImport("User 32.dll")]
public static extern Int32 GetWindowText(i nt hWnd, StringBuilder s,
int nMaxCount);
[DllImport("User 32.dll")]
public static extern Int32 GetWindowTextLe ngth(int hwnd);
[DllImport("user 32.dll", EntryPoint = "GetDesktopWind ow")]
public static extern int GetDesktopWindo w();
[DllImport("User 32.dll")]
public static extern void keybd_event(byt e bVk, byte bScan, int
dwFlags, int dwExtraInfo);
}

Nov 17 '05 #13

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

Similar topics

2
1904
by: rob | last post by:
Dear All, I am getting an error "Illegal cross-thread operation". I am not creating any threads. I know internally there are many threads running, though. Does that mean that no form can call a function from another form even if there is a parent-child relationship? Aren't controls like TreeView based on forms. If so then why can a form containing a TreeView call members of the TreeView? If you're interested here is my scenario:
12
2380
by: [Yosi] | last post by:
What I should do to return back the permissin I had in VS2003 , that allowd me to access public methode of one Class from other Thread. I have Class A(FORM) which create new thread (new class B), the new class thread get as parameter reference to his father( CLASS who mad it (classA)), The new thread need to call methodes from his father like myfathe.methode(), This worked in VS 2003 but now in VS 2005 Beta throw an exception : An...
4
4441
by: oran johnson | last post by:
Illegal cross-thread operation: Control 'PopupInfo' accessed from a thread other than the thread it was created on. Can I track this down using the debugger? Thanks.
11
9181
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...
8
4858
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
18
6609
by: Dave Booker | last post by:
I have a Thread making a callback to a Form function that alters Form data. Of course, the debugger is telling me, "Cross-thread operation not valid: Control 'FormTest' accessed from a thread other than the thread it was created on." But I am locking before I touch the shared data, so do I actually need to worry about this, or is it just the debugger being oversimplistic? Specifically, I have:
0
9685
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10886
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...
0
10277
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
9413
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
7817
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
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
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.