PostMessage的非法交叉线程操作 [英] illegal cross thread operation by PostMessage

查看:92
本文介绍了PostMessage的非法交叉线程操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




使用P / Invoke我使用PostMessage从一个线程向另一个线程发送消息

。我很确定这已经在dotnet中运行了。但是几天前我已经升级了2版本。


现在我发布了一条非法的交叉线程操作。这是在最新版本的dotnet中引入的

错误吗?使用

postmessage是非常合法的,因为该消息将在

发布的帖子的上下文中执行。与SendMessage相同。


有人可以启发这是什么意思吗?


-

rgds,Wilfried
http://www.mestdagh.biz

推荐答案

Wilfried,


如果没有看到代码,很难说。你能发布你b $ b使用的代码吗?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" ; Wilfried Mestdagh <无线************** @ discussions.microsoft.com>写在

消息新闻:4E ********************************** @ microsof t.com ...
Wilfried,

Without seeing the code, it is hard to say. Can you post the code you
are using?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com> wrote in
message news:4E**********************************@microsof t.com...


使用P / Invoke我使用PostMessage从一个线程向另一个线程发送消息。我很确定这已经在dotnet中运行了。但我几天前已经升级了版本2.

现在我发布了一条非法的跨线程操作。在最新版本的dotnet中引入了这个
错误吗?使用
postmessage是非常合法的,因为消息将在发布的帖子的上下文中执行。与SendMessage相同。

有人可以启发这是什么意思吗?

rgds,Wilfried
http://www.mestdagh.biz



您好Nicholas,
Hi Nicholas,
没有看到代码,很难说。你可以发布你正在使用的代码吗?
Without seeing the code, it is hard to say. Can you post the code you
are using?




我目前还没有解决使用Invoke的事件,但仍然是

想知道错误。这是代码。我试着尽可能清楚地解释。


当收到一个特定数据包

时,在TCP套接字的EndRecieve之后调用它。这当然不在主线程上下文中:


private void DoDataAvailable(byte [] buffer,int offset,int count)

{

if(count< = 2)

return;

count - = 2;

string rcvd = Encoding.UTF8。 GetString(buffer,offset,count);

switch(rcvd){

case UPDATE:

Main.Globals.showVehicles.WMUpdate() ;


showVehicles和Globals是一个在主表单中定义如下的表单来获取它

可以作为全球访问:


命名空间TTLocator

{

partial class Main:Form

{

public ShowVehicles showVehicles = new ShowVehicles ();

静态公共主要全局;


和形式构造函数:

Globals = this;

在WhowVehicles中的
这是被调用的程序:


const int WM_UPDATE = 0x401;

public void WMUpdate( )

{

Win32.PostMessage(句柄,WM_)更新,0,0);

}


这也是跨线程异常的来源。 Win32是一个

clas,其中我使用了最常用的Win32 API调用.PostMessage是好的

旧的用于crott线程消息:)


我发布的帖子足以让你看到我的问题吗?


thx Wilfried



I have dont workaround with an event using Invoke at the moment, But still
wonder wy the error. Here is the code. I try to explain as clear as possible.

This is called after an EndRecieve of a TCP socket when a certain packet
completely is received. this is of course not in main thread context:

private void DoDataAvailable(byte[] buffer, int offset, int count)
{
if (count <= 2)
return;
count -= 2;
string rcvd = Encoding.UTF8.GetString(buffer, offset, count);
switch (rcvd) {
case UPDATE:
Main.Globals.showVehicles.WMUpdate();

showVehicles and Globals is a form defined as follow in Main form to get it
accesible as a global:

namespace TTLocator
{
partial class Main: Form
{
public ShowVehicles showVehicles = new ShowVehicles();
static public Main Globals;

and in constructor of form:

Globals = this;

in WhowVehicles this is the called procedure:

const int WM_UPDATE = 0x401;
public void WMUpdate()
{
Win32.PostMessage(Handle, WM_UPDATE, 0, 0);
}

And this is also where the cross thread exception is coming from. Win32 is a
clas where I have my most used Win32 API calls in. PostMessage the the good
old one used to crott thread messages :)

Do I post enough to let you see my problem ?

thx Wilfried


Wilfried,


继续使用Control.Invoke将代码的执行封送到

UI线程中。这不是一种解决方法。这是标准。


你得到什么错误?如果它是一个

异常,可以发布堆栈跟踪吗?什么代码处理你的用户定义的WM_UPDATE

消息?它是IMessageFilter吗?无论哪种方式,它都可能都没什么问题

因为我认为你无论如何都不需要使用PostMessage。


Brian


Wilfried Mestdagh写道:
Wilfried,

Continue using Control.Invoke to marshal the execution of code onto the
UI thread. It''s not a workaround. It''s the standard.

What error are you getting? Can post the stack trace if it is an
exception? What code is processing your user defined WM_UPDATE
message? Is it an IMessageFilter? Either way it''s probably all moot
because I don''t think you need to be using PostMessage anyway.

Brian

Wilfried Mestdagh wrote:
我目前还没有使用Invoke处理事件,但是
仍然怀疑是错误的。这是代码。我试着尽可能清楚地解释。

[snip]

thx Wilfried
I have dont workaround with an event using Invoke at the moment, But
still wonder wy the error. Here is the code. I try to explain as
clear as possible.

[snip]

thx Wilfried






这篇关于PostMessage的非法交叉线程操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆