Getting "Value does not fall within the expected range." on a call to EventWaitHandle.WaitAll(WaitHandle[], Int32)

Kamen 45 Reputation points
2024-06-22T00:29:01.43+00:00

In my code I make a call (from an MTA thread) to EventWaitHandle.WaitAll(WaitHandle[], Int32) and get the exception: "Value does not fall within the expected range." All the handles in the array (three of them at this time) are non-null, valid and open (the underlying SafeWaitHandle). There are no duplicates, and the timeout is 50. There are no mutexes involved and all of the EventWaitHandle are local (freshly opened from the same thread, no transparent proxies). Anyway, if any of those were a problem, I'd be getting a different exception. The System.ArgumentException is not even listed in the documentation of WaitAll. The arguments I'm passing are an array of EventWaitHandle (containing three elements) and the integer value "50". What could be wrong with them? EventWaitHandle derives from System.Threading.WaitHandle, a small positive integer is an integer - what else? Needless to say, I've Googled the crap out of this - no results. When I use WaitOne, it works just fine (except, it's not what I want). Any thoughts? Thanks!

Kamen

Here's the few lines of code where this is implemented:

List<EventWaitHandle> AllWHList = new List<EventWaitHandle>();
EventWaitHandle[] AllWH = new EventWaitHandle[ChannelsToWaitOn.Count];
foreach(Guid ChID in ChannelsToWaitOn)  // Wait for value updates on all specified channels (this _will block_, but will also continuously check for request to abort execution)
{
  EventWaitHandle EWH = RefProgram.GetChannelByID(ChID).RegisterForValueUpdateNotification(OUID);
  if(EWH != null) // This will happen as the program gets built
    AllWHList.Add(EWH);
}
if(AllWHList.Count > 0) // No more than 64!
{
  AllWH = AllWHList.ToArray();
  while(!EventWaitHandle.WaitAll(AllWH, (int)(ResolutionSec*1000)) && !AbortExecution) { }  // This just waits (for success or abort request); no action
}

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,917 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2024-06-24T12:49:00.6+00:00

    Hi @Kamen , Welcome to Microsoft Q&A,

    The System.ArgumentException you are encountering with EventWaitHandle.WaitAll(WaitHandle[], Int32) can indeed be perplexing, especially since the arguments appear to be valid.

    List<EventWaitHandle> AllWHList = new List<EventWaitHandle>();
    EventWaitHandle[] AllWH = new EventWaitHandle[ChannelsToWaitOn.Count];
    
    foreach (Guid ChID in ChannelsToWaitOn)
    {
        EventWaitHandle EWH = RefProgram.GetChannelByID(ChID).RegisterForValueUpdateNotification(OUID);
        if (EWH != null)
        {
            AllWHList.Add(EWH);
        }
    }
    
    if (AllWHList.Count > 0)
    {
        AllWH = AllWHList.ToArray();
    
        foreach (var handle in AllWH)
        {
            Console.WriteLine($"Handle: {handle}, SafeWaitHandle: {handle.SafeWaitHandle}, IsClosed: {handle.SafeWaitHandle.IsClosed}, IsInvalid: {handle.SafeWaitHandle.IsInvalid}");
        }
    
        if (AllWH.Length > 64)
        {
            throw new InvalidOperationException("Too many wait handles. Maximum allowed is 64.");
        }
    
        try
        {
            while (!EventWaitHandle.WaitAll(AllWH, (int)(ResolutionSec * 1000)) && !AbortExecution)
            {
                // Wait loop
            }
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine("ArgumentException: " + ex.Message);
            // Additional logging or handling
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.