线程问题(令人难以置信的直接互斥使用不起作用) [英] Threading problem (incredibly straightforward mutex usage not working)

查看:111
本文介绍了线程问题(令人难以置信的直接互斥使用不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遇到奇怪的线程冲突,并且在移动相关的

互斥锁后,我终于确定互斥锁没有
似乎一直在工作。


以下是相关代码:


private static Mutex previewMutex = new Mutex();


私有静态无效预览(字符串来源,字符串目标)

{

尝试

{

previewMutex.WaitOne();

if(previewerThread!= null&& previewerThread.IsAlive)

{

evl.WriteEntry(" WaitingForPreviewerThread");

previewerThread.Join();

evl.WriteEntry(" FinishedWaiting");

}

DocSearch.previewSource = source;

DocSearch.previewTarget = target;

previewerThread = new Thread(DocSearch.previewStarter);

previewerThread.Priority = ThreadPriority.Normal;

previewerThread.Name ="预览下载器;


previewerThread.Start();

previewMutex.ReleaseMutex();

}

catch(异常错误)

{

evl.WriteEntry(err.ToString());

}

}

正在发生的事情是互斥锁未正确阻塞。我查看

事件日志,并在

行中查看WaitingForPreviewerThread消息三次,其间根本没有消息。我已经验证了日志消息

在代码中的其他任何地方都没有,这是唯一的地方previewMutex

被使用。真正搞砸的是,这个函数只是从GUI事件处理程序(ListView的OnChange事件)调用的。
。来自

我对MFC和Win32编程的理解,GUI应该

默认只有一个活动线程,这意味着这不应该是

可能与互斥量无关。如果

用户慢慢浏览列表框,包括所有预期的事件

日志消息,代码按预期工作。


这里这就是线程的作用,如果重要的话:


public static void backgroundPreview()

{

try

{

Common.GetFile(DocSearch.previewSource,DocSearch.p reviewTarget);


Common.SendMessage(Common.mainHwnd,Common .WM_PREVIEWREADY,0,0);

}

catch(异常错误)

{

evl.WriteEntry (err.ToString());

}

}


GetFile调用Web服务并下载文件。 SendMessage是

win32 API。

WM_PREVIEW的处理程序在激活

线程的同一表单类中,它显示一张图片在activeX控件中。


非常感谢任何帮助!

Tyler

***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

I''ve been having strange thread conflicts, and after moving the relevant
mutex higher and higher I''ve finally determined that the mutex doesn''t
seem to be working at all.

Here is the relevant code:

private static Mutex previewMutex = new Mutex();

private static void preview (string source, string target)
{
try
{
previewMutex.WaitOne();
if (previewerThread != null && previewerThread.IsAlive)
{
evl.WriteEntry("WaitingForPreviewerThread");
previewerThread.Join();
evl.WriteEntry("FinishedWaiting");
}
DocSearch.previewSource = source;
DocSearch.previewTarget = target;
previewerThread = new Thread (DocSearch.previewStarter);
previewerThread.Priority = ThreadPriority.Normal;
previewerThread.Name = "PreviewDownloader";

previewerThread.Start();
previewMutex.ReleaseMutex();
}
catch(Exception err)
{
evl.WriteEntry(err.ToString());
}
}
What''s happening is the mutex is not blocking properly. I look in the
event log and see the WaitingForPreviewerThread message three times in a
row, with no messages at all in between. I''ve verified that log message
isn''t anywhere else in the code and this is the only place previewMutex
is used. The really messed up thing is that this function is only
called from a GUI event handler (OnChange event for a ListView). From
my understanding from doing MFC and Win32 programming, the GUI should
only have one active thread by default anyway, meaning this shouldn''t be
possible regardless of the mutex. The code works as expected if the
user slowly arrows through the listbox including all the expected event
log messages.

Here''s what the thread does, if it matters:

public static void backgroundPreview()
{
try
{
Common.GetFile(DocSearch.previewSource,DocSearch.p reviewTarget);

Common.SendMessage (Common.mainHwnd, Common.WM_PREVIEWREADY, 0, 0);
}
catch (Exception err)
{
evl.WriteEntry(err.ToString());
}
}

GetFile calls a web service and downloads a file. SendMessage is the
win32 API.
The handler for WM_PREVIEW is in the same form class that activates the
thread, and it displays a picture in an activeX control.

Any help is greatly appreciated!

Tyler
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

Tyler Sample< st ** @ qwest.net>写道:
Tyler Sample <st**@qwest.net> wrote:
我一直在遇到奇怪的线程冲突,并且在将相关的互斥锁移到更高和更高的位置之后我终于确定互斥锁没有
似乎一直都在工作。
I''ve been having strange thread conflicts, and after moving the relevant
mutex higher and higher I''ve finally determined that the mutex doesn''t
seem to be working at all.




你能发一个简短但完整的程序来演示

问题吗?


请参阅 http://www.pobox。 com / ~siget / csharp / complete.html 了解详情

我的意思是什么。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


如果没有其他线程获取互斥锁,则Mutex.WaitOne不会阻塞。您不会显示任何其他尝试获取互斥锁的线程。您对Mutex的期望是什么?


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


我'一直有奇怪的线程冲突,并且在移动相关的

mutex越来越高后我终于确定互斥锁没有b
似乎工作正常好吧。


< snip>
The Mutex.WaitOne will not block if no other thread has acquired the mutex. You don''t show any other thread attempting to acquire the mutex. What behavior were you expecting from the Mutex?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I''ve been having strange thread conflicts, and after moving the relevant
mutex higher and higher I''ve finally determined that the mutex doesn''t
seem to be working at all.

<snip>


以某种方式预览()输入两到三次而不退出

在Join()之前阻塞并锁定整个应用程序。


我会尝试做一个演示问题的示例应用程序..

***通过开发人员指南 ht发送tp://www.developersdex.com ***

不要只是参加USENET ......获得奖励!
Somehow preview() is being entered two to three times without exiting
before the Join() blocks and locks up the whole application.

I''ll try to do a sample application that demonstrates the problem..
*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于线程问题(令人难以置信的直接互斥使用不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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