Search code examples
c#.netproxy.net-3.5titanium-web-proxy

what is the equivalent of Task.FromResult() from .NET 4.5 in .NET 3.5


I'm using the titanium proxy to analyse the data trafic.

They using Task.FromResult(0) as return. My environment it was 3.5 framework.

 m_proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;

what can we use replace for Tasks.FromResult(0) in .NET3.5 framework, Because my environment works in 3.5 framework only.

public Task OnCertificateValidation(object sender,CertificateValidationEventArgs e)
    {           
        //set IsValid to true/false based on Certificate Errors
        e.IsValid = true;         

        return Task.FromResult(0);
    }

I have gone through the question 40422779, But I cannot able to use async since its only .NET 3.5.


Solution

  • This answer is inspired from Link

    I have created a new class and named as Tasks and implemented the following

    public static class Tasks
    {
        public static Task<TResult> FromResult<TResult>(TResult result)
        {
            var tcs = new TaskCompletionSource<TResult>();
            tcs.SetResult(result);
            return tcs.Task;
        }
    
        public static Task WhenAll(Task[] tasks)
        {
            return Task.Factory.ContinueWhenAll(tasks, (t) => t);
        }
    
        public static Task Delay(int millisecondsDelay)
        {
            var tcs = new TaskCompletionSource<object>();
            new Timer(_ => tcs.SetResult(null)).Change(millisecondsDelay, -1);
            return tcs.Task;
        }
    }
    

    Then I changed the main method as

     public Task OnCertificateValidation(object sender,CertificateValidationEventArgs e)
    {           
        //set IsValid to true/false based on Certificate Errors
        e.IsValid = true;         
    
        return Tasks.FromResult(0);
    }