473,785 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading problem (incredibly straightforward mutex usage not working)

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.Wa itOne();
if (previewerThrea d != null && previewerThread .IsAlive)
{
evl.WriteEntry( "WaitingForPrev iewerThread");
previewerThread .Join();
evl.WriteEntry( "FinishedWaitin g");
}
DocSearch.previ ewSource = source;
DocSearch.previ ewTarget = target;
previewerThread = new Thread (DocSearch.prev iewStarter);
previewerThread .Priority = ThreadPriority. Normal;
previewerThread .Name = "PreviewDownloa der";

previewerThread .Start();
previewMutex.Re leaseMutex();
}
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 WaitingForPrevi ewerThread 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 backgroundPrevi ew()
{
try
{
Common.GetFile( DocSearch.previ ewSource,DocSea rch.previewTarg et);

Common.SendMess age (Common.mainHwn d, Common.WM_PREVI EWREADY, 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!
Nov 16 '05
11 3087
Oh yeah...That's true! So, I guess that leaves me some hope that
everything is working okay, although...

I'm a little vague on how I could have a legitimate re-entrancy problem
in the same thread between these lines of code:

evl.WriteEntry( "WaitingForPrev iewerThread\r\n "+AppDomain.Get CurrentThrea
dId().ToString( ));
previewerThread .Join();
evl.WriteEntry( "FinishedWaitin g");

It doesn't really seem that there's any way for the thread to escape
there, not matter what I may be doing elsewhere..I could certainly see
how a *different* thread could enter again...but the same one? None of
these calls are wrapped. They are straight system calls..No opportunity
to re-enter the message loop as far as I can tell! I'm catching
exceptions and logging them, and there aren't any, so....

If I'm incorrect there, I would love to read a technical article
explaining my mistake.

Also, does Join() work like a WaitForSingleOb ject(threadHand le) call did
in C++? Or is there some deviousness possible in that call?

Thanks for all the help so far. I'm definitely much closer to solving
this problem than before...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11
Okay, I found the problem. This was referred to earlier, but I assumed
it mentioned as deliberate behavior on the part of the controls I was
calling. I didn't realize that Microsoft built in message pumping to
their blocking calls. I'm particularly thrilled that they didn't
document it at all! Wow. I'm ALMOST speechless :) Great. Now I have
to synchronize my main application thread with itself. I see why they
did it, but I have no idea why it's not clearly documented in the
relevant function calls.

http://blogs.msdn.com/cbrumme/archiv.../02/66219.aspx

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12

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

Similar topics

0
1761
by: Alina | last post by:
Hello I'm working with a disconnected DataSet and I wish to make thread-safe the "add new rows" method. I try like this, but does not work: public class MyXmlDataDoc { ..... System.Threading.Mutex m = null;
3
2817
by: ron | last post by:
If I use the System.Threading.ThreadPool.QueueUserWorkItem to spawn a new threaded process, will this process be executed regardless of whether or not the current process ends prior to completion of the child thread? Or do I need to create a new thread object and join it to my current thread in order to guarantee execution of code in a child process? -Ron
5
1692
by: ste | last post by:
this code not work: it is no possible Exit from Monitor Why ? thank ste //////// using System; using System.Collections;
4
4094
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from the "EventLog" object in the "System.Diagnostics" namespace. When a certain event occurrs, both a WMI event is raised, and an event log entry is written. The problem I have is that I need to capture both events and somehow correlate which WMI...
16
351
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread are writing to the same object, whats the best way to manage this ? Thanks OHM --
17
1454
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Assumes a Form with a Panel on it., Does the Mutex have to be within the address of a thread start address ? Cheers - OHM '----------- *************** ---------------- Private endProgram As Boolean = False Dim image1 As Image = Image.FromFile("..\Images\gun.bmp")
6
1320
by: MobileBoy36 | last post by:
Hi All, I want to make a LogFile class that is thread safe. I use a Mutex for it. But the behavior of the class is not that normal. In a c# guide I read you can achieve it by simply using Mutex.Waitone and Mutex.ReleaseMutex. Is that right? What 's wrong with my sub Writelog then? Best regards, Mobileboy
2
2060
by: Jeroen | last post by:
Hi all, I've been trying to get my head around threading. Here's an example from the book I'm reading: /***************************/ Mutex m = null; const string name = "xyz"; try
126
6750
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
9647
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
9491
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
10357
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
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7510
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
5397
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...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.