waithandle

Run Message Loop while waiting for WaitHandle

Is there any way to process all Windows messages while the UI thread is waiting on a WaitHandle or other threading primitive? I realize that it could create very messy reentrancy problems; I want to do it anyway. EDIT: The wait occurs in the middle of a complicated function that must run on the UI thread. Therefore, moving the wait to...

Can a call to WaitHandle.SignalAndWait be ignored for performance profiling purposes?

I just downloaded the trial version of ANTS Performance Profiler from Red Gate and am investigating some of my team's code. Immediately I notice that there's a particular section of code that ANTS is reporting as eating up to 99% CPU time. I am completely unfamiliar with ANTS or performance profiling in general (that is, aside from self...

For a windows service, which is better, a wait-spin or a timer?

This question about Timers for windows services got me thinking: Say I have (and I do) a windows service thats waiting on a WaitHandle and when woken, it dives into a wait-spin like I have shown below in a flowchart I'm curious if using a timer would be better than a wait-spin loop (sometimes called a spin-wait). I'll be honest, I'v...

WaitHandle.WaitAny and Semaphore class

Edit: I'd like to plead temporary insanity for even asking this question, but it made sense at the time (see edit 2 below). For a .NET 3.5 project, I have two types of resources (R1 and R2) that I need to check the availability of. Each resource type can have (say) 10 instances at any time. When one of either types of resources becomes...

Is mutex correctly implemented and how do I dispose it?

I am reviewing some code and one of the code analysis (fxCop) warnings has gotten me very confused. The code implements a few mutex's by creating variables at the start of the class, similar to this: private Mutex myMutex = new Mutex(); fxCop is popping up with a message saying that I must implement IDisposable for the class as the Mu...

WaitHandle.WaitAny() & WaitHandle.WaitAll() usage problem...

My application is not exiting properly. I am just trying to print the total number of connections, after that waiting for all the upload operations to complete, and then quit gracefully. Below is the code... using System; using System.Net; using System.Threading; using System.IO; using System.Text; namespace ServicePointDemo { cla...

My EventWaitHandle says "Access to the path is denied", but its not

Quick summary with what I now know I've got an EventWaitHandle that I created and then closed. When I try to re-create it with this ctor, an "Access to the path ... is denied" exception is thrown. This exception is rare, most of the times it just re-creates the EventWaitHandle just fine. With the answer posted below (by me), I'm able ...

AJAX - timed mySQL queries (please wait screen)

I need to make an AJAX page which queries the database on page load and then every 5-10 seconds after that. In the meantime I will display some kind of waiting page (maybe with a animated gif to keep my customers entertained :) ) I am working with paypals IPN so its for while I am waiting for the transaction to clear.. most of the tim...

Producer Consumer With AutoResetEvent

I'm trying to use the producer consumer pattern to process and save some data. I'm using AutoResetEvent for signalling between the two therads here is the code I have Here is the producer function public Results[] Evaluate() { processingComplete = false; resultQueue.Clear(); for (int i = 0; i < data.Length...

Multi-Threading - waiting for all threads to be signalled

I have scenarios where I need a main thread to wait until every one of a set of possible more than 64 threads have completed their work, and for that I wrote the following helper utility, (to avoid the 64 waithandle limit on WaitHandle.WaitAll()) public static void WaitAll(WaitHandle[] handles) { if (handles == null) ...

A robust method of tracking failed workers with ThreadPool

I'm looking for a good method of tracking (counting) which workers have failed when queued with a Threadpool and using WaitHandle.WaitAll() for all threads to finish. Is Interlocking a counter a good technique or is there a more robust strategy? ...

Running multiple threads, starting new one as another finishes.

I have an application that has many cases. Each case has many multipage tif files. I need to covert the tf files to pdf file. Since there are so many file, I thought I could thread the conversion process. I'm currently limiting the process to ten conversions at a time (i.e ten treads). When one conversion completes, another should s...

Do I need to call Close() on a ManualResetEvent?

I've been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading the documentation for WaitHandle, I saw the following: WaitHandle implements the Dispose pattern. See Implementing Finalize and Dispose to Clean Up Unmanaged ...

Explanation of Text on Threading in "C# 3.0 in a Nutshell"

While reading C# 3.0 in a Nutshell by Joseph and Ben Albahari, I came across the following paragraph (page 673, first paragraph in section titled "Signaling with Wait and Pulse") "The Monitor class provides another signalling construct via two static methods, Wait and Pulse. The principle is that you write the signalling logic yourse...

Server multithreading overkill?

I'm creating a server-type application at the moment which will do the usual listening for connections from external clients and, when they connect, handle requests, etc. At the moment, my implementation creates a pair of threads every time a client connects. One thread simply reads requests from the socket and adds them to a queue, an...

C#: How to set AsyncWaitHandle in Compact Framework?

Hi, I'm using a TcpClient in one of my Compact Framework 2.0 applications. I want to receive some information from a TCP server. As the Compact Framework does not support the timeout mechanisms of the "large" framework, I'm trying to implement my own timeout-thing. Basically, I want to do the following: IAsyncResult result = networkSt...

Why doesn't my NamedPipeServerStream wait??

I'm working with a NamedPipeServerStream to communicate between two processes. Here is the code where I initialize and connect the pipe: void Foo(IHasData objectProvider) { Stream stream = objectProvider.GetData(); if (stream.Length > 0) { using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("Visualiz...

Workaround for the WaitHandle.WaitAll 64 handle limit?

My application spawns loads of different small worker threads via ThreadPool.QueueUserWorkItem which I keep track of via multiple ManualResetEvent instances. I use the WaitHandle.WaitAll method to block my application from closing until these threads have completed. I have never had any issues before, however, as my application is comin...

How-to dispose a waithandle correctly

Hello, I'm doing some multi-threading and use AutoResetEvents and ManualResetEvents do control my main - loop. When "destryoing" the threads I also have to dispose these signals, that's clear. But I saw different ways how to dispose Waithandles, and I'm not sure which one is correct: Version 1 if (disposing) { this.threadExitEvent.S...

How should I implement the C# server side portion of long-polling for ajax requests?

I've got an architecture that involves browsers polling via ajax every 3 seconds for updates and I'd like to change that to long-polling. I'd like to have 1, 2.. {n} clients long-polling, waiting for updates and have something happen on the server to signal the waiting clients to return. My first thought was to use an EventWaitHandle, ...