Why do I get a task cancelled exception using a Dispatcher like this?

-1

I have a small program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThreadingTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var result = Threadedthing.Instance.GetThing();
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

Threadedthing looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace ThreadingTest
{
    public class Threadedthing
    {
        private static Threadedthing _instance;
        private static object _instanceLock = new object();
        public static Threadedthing Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_instanceLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Threadedthing();
                        }
                    }
                }

                return _instance;
            }
        }

        private Thread _thread;
        private Dispatcher _dispatcher;

        private Threadedthing()
        {
            var tcs = new TaskCompletionSource<Dispatcher>();
            _thread = new Thread(new ThreadStart(() => { tcs.SetResult(Dispatcher.CurrentDispatcher);}));
            _thread.Start();

            _dispatcher = tcs.Task.GetAwaiter().GetResult();
        }

        public string GetThing()
        {
            if (Thread.CurrentThread != _thread)
            {
                return _dispatcher.Invoke<string>(GetThing);
            }

            return "woo";
        }
    }
}

When I run it, the program hangs.

When I debug it, I get an exception when I try to call Invoke.

System.Threading.Tasks.TaskCanceledException
  HResult=0x8013153B
  Message=A task was canceled.
  Source=mscorlib
  StackTrace:
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.Invoke[TResult](Func`1 callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.Invoke[TResult](Func`1 callback)
   at ThreadingTest.Threadedthing.GetThing() in C:\Users\antho\source\repos\ThreadingTest\ThreadingTest\Threadedthing.cs:line 50
   at ThreadingTest.Program.Main(String[] args) in C:\Users\antho\source\repos\ThreadingTest\ThreadingTest\Program.cs:line 13

This sentence is just to get around the "It looks like your post is mostly code; please add some more details.". I will repeat this message until it works.

c#
.net
multithreading
dispatcher
asked on Stack Overflow Jul 26, 2019 by AnthonyM

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0