John Carpenter John Carpenter - 1 year ago 99
C# Question

Readonly WaitHandle C#

I am exposing a

on an interface for an object that's responsible for creating a resource internally, but it may take a long time. The contract looks like this:

public interface IHaveExpensiveResources {
WaitHandle FinishedInitialization { get; }

starts unset, but then I set it once the initialization completes (in the background, after the constructor returns). The thing I don't like about this is that consumers of this contract can set the
themselves, when they have no business being able to do this. It reminds me of the difference between a get-only
property and a

Is there anything in C# like a
which only exposes the
overloaded methods?

So that I'm not a victim of the XY problem, is there a more canonical way of asynchronously constructing objects which have long construction times?

Answer Source

Do you have to pass back a WaitHandle or could you make your own wrapper class that only exposes the functions you want, similar to what ReadOnlyCollection does?

One option is to wrap the WaitHandle up in to a task (Code example taken from the MSDN)

public static Task WaitOneAsync(this WaitHandle waitHandle)
    if (waitHandle == null) 
        throw new ArgumentNullException("waitHandle");

    var tcs = new TaskCompletionSource<bool>();
    var rwh = ThreadPool.RegisterWaitForSingleObject(waitHandle, 
        delegate { tcs.TrySetResult(true); }, null, -1, true);
    var t = tcs.Task;
    t.ContinueWith( (antecedent) => rwh.Unregister(null));
    return t;

Then you just call .WaitOneAsync() on whatever FinishedInitialization would have been returning and return that Task instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download