Shahar Prish
Shahar Prish

Reputation: 4847

How do I get a callback from a ManualResetEvent in a Universal app?

In a universal (windows 10) app, I want to get a callback when a WaitHandle is signaled (ManualResetEvent in my case). How do I achieve this? Usually I would use ThreadPool.RegisterWaitForSingleObject() but alas, ThreadPool went the way of the dodo.

Any other (efficient - non thread blocking) alternatives?

Upvotes: 0

Views: 593

Answers (1)

Jeffrey Chen
Jeffrey Chen

Reputation: 4690

The ThreadPool class in .Net for UWP does not have the method RegisterWaitForSingleObject.

As Hans suggested, you can start a new thread to wait for the signal to workaround.

    private static ManualResetEvent mre = new ManualResetEvent(false);

    public MainPage()
    {
        this.InitializeComponent();

        Task.Run(() => {
            mre.WaitOne();

            Debug.WriteLine("do sth else"); 
        });
    }



    private void button_Click(object sender, RoutedEventArgs e)
    {
        mre.Set(); 
    }

Upvotes: 1

Related Questions