Bug 13519 - ThreadPool.RegisterWaitForSingleObject WaitOrTimerCallback is called without the wait or timeout
Summary: ThreadPool.RegisterWaitForSingleObject WaitOrTimerCallback is called without ...
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.4.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-07-26 13:14 UTC by dj_technohead
Modified: 2013-07-30 14:14 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description dj_technohead 2013-07-26 13:14:19 UTC

    
Comment 1 dj_technohead 2013-07-26 13:15:35 UTC
In Xamarin.iOS 6.2.7 a call to ThreadPool.RegisterWaitForSingleObject would work as expected, but after updating to 6.4, the WaitOrTimerCallback is called immediately without the expected waiting or time out.
Comment 2 dj_technohead 2013-07-26 15:55:53 UTC
Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(requestClass.webRequest.BeginGetResponse, requestClass.webRequest.EndGetResponse, null);


								ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, 
								                                       new WaitOrTimerCallback((state, timedOut) => { 
									
										Console.WriteLine("Timeout: " + timedOut);

timedOut == false here.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2013-07-29 18:52:47 UTC
Marek, can you have a look at this?
Comment 4 Marek Safar 2013-07-30 05:32:24 UTC
I tried following program which works as expected. It prints "Timeout: False" because 3000 ms timeout was not hit RegisterWaitForSingleObject queued the delegate almost immediately because AsyncWaitHandle handle was set. If this code does not reproduce your issue please include full C# source code which does. 

using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;


class C
{
    public static void Main()
    {
        var wr = WebRequest.Create("http://www.google.com");
        Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(wr.BeginGetResponse, wr.EndGetResponse, null);

        ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, new WaitOrTimerCallback((state, timedOut) => { 
            Console.WriteLine("Timeout: " + timedOut);
        }), "state", 3000, true);
 
        Console.ReadKey();

    }
}
Comment 5 dj_technohead 2013-07-30 14:14:02 UTC
Here's the full code that I'm using:
Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(requestClass.webRequest.BeginGetResponse, requestClass.webRequest.EndGetResponse, null);
								
ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, 
                                       new WaitOrTimerCallback((state, timedOut) => { 

	try
	{
		Console.WriteLine("Timeout: " + timedOut);
		if (timedOut)
		{
			Console.WriteLine("Timeout");
			WebRequest request = (WebRequest)state;
			if (state != null)
			{
				request.Abort();
			}
		}
	
		requestClass.request.Time = DateTime.Now - requestClass.request.StartTime;
		requestClass.requestCallback(HttpRequestResult.TimedOut);
	}
	finally
	{
		Interlocked.Decrement(ref currentNumSimultaneousRequests);
	}
}), requestClass.webRequest, requestClass.request.TimeoutMilisecs, true);
								asyncTask.ContinueWith(task =>
								                       {
	try
	{
		requestClass.webResponse = (HttpWebResponse)task.Result;
	
		requestClass.request.Time = DateTime.Now - requestClass.request.StartTime;
	
	
	
		byte[] response = null;
		HttpRequestResult result = HttpRequestResult.Success;
	
		if (requestClass.webResponse != null)
		{
			requestClass.statusCode = requestClass.webResponse.StatusCode;
		}
	
		requestClass.requestCallback(result);
	}
	finally	
	{
		Interlocked.Decrement(ref currentNumSimultaneousRequests);
	
	}
}, TaskContinuationOptions.NotOnFaulted);
// Handle errors
asyncTask.ContinueWith(task =>
{
	requestClass.request.Exception = task.Exception;
	requestClass.request.InnerException = task.Exception.InnerException;

	// Track whether you cancelled or not... up to you...

	Interlocked.Decrement(ref currentNumSimultaneousRequests);

	requestClass.requestCallback(HttpRequestResult.Error);
								}, TaskContinuationOptions.OnlyOnFaulted);


At any rate, the problem is no longer an issue for me, as I rewrote this to use the new System.Net.Http framework. But I brought this up as the behavior of the above code is different under 6.4 from 6.2.7. Unfortunately I did not step through to see what the difference in behavior was, exactly.