예제 #1
0
파일: ServiceImpl.cs 프로젝트: wasabii/rnet
        /// <summary>
        /// Invoke this method upon start.
        /// </summary>
        /// <param name="args"></param>
        public void OnStart(string[] args)
        {
            Contract.Requires<ArgumentNullException>(args != null);

            var sect = RnetServiceConfigurationSection.GetDefaultSection();
            if (sect == null)
                throw new ConfigurationErrorsException("Rnet.Service configuration not found.");

            // spawn each host
            foreach (var conf in sect.Hosts)
            {
                // expose new bus on a new host with new container
                var container = new CompositionContainer(catalog);
                var context = new AsyncContextThread();
                var bus = new RnetBus(conf.Bus);
                var host = new RnetHost(bus, conf.Uri, container);
                instances.Add(new Instance(container, context, bus, host));

                // schedule initialization
                context.Factory.Run(async () =>
                {
                    await bus.Start();
                    await host.StartAsync();
                }).Wait();
            }
        }
        public void ProgressReport_NotifiesChangeOnCapturedSynchronizationContext()
        {
            Test.Async(async () =>
            {
                SynchronizationContext updateContext = null;
                SynchronizationContext threadContext = null;

                var tcs = new TaskCompletionSource();
                using (var thread = new AsyncContextThread())
                {
                    threadContext = await thread.Factory.Run(() => SynchronizationContext.Current);
                    PropertyProgress<int> propertyProgress = await thread.Factory.Run(() => new PropertyProgress<int>());
                    propertyProgress.PropertyChanged += (_, e) =>
                    {
                        updateContext = SynchronizationContext.Current;
                        tcs.SetResult();
                    };
                    IProgress<int> progress = propertyProgress;
                    progress.Report(13);
                    await tcs.Task;
                }

                Assert.IsNotNull(updateContext);
                Assert.AreEqual(threadContext, updateContext);
            });
        }
 public void Join_StopsTask()
 {
     AsyncContext.Run(async () =>
     {
         var context = new AsyncContextThread();
         var thread = await context.Factory.Run(() => Thread.CurrentThread);
         await context.JoinAsync();
     });
 }
 public void Context_IsCorrectAsyncContext()
 {
     AsyncContext.Run(async () =>
     {
         using (var thread = new AsyncContextThread())
         {
             var observedContext = await thread.Factory.Run(() => AsyncContext.Current);
             Assert.AreSame(observedContext, thread.Context);
         }
     });
 }
 public void AsyncContextThread_IsAnIndependentThread()
 {
     AsyncContext.Run(async () =>
     {
         var testThread = Thread.CurrentThread.ManagedThreadId;
         var thread = new AsyncContextThread();
         var contextThread = await thread.Factory.Run(() => Thread.CurrentThread.ManagedThreadId);
         Assert.AreNotEqual(testThread, contextThread);
         await thread.JoinAsync();
     });
 }
예제 #6
0
파일: ServiceImpl.cs 프로젝트: wasabii/rnet
            /// <summary>
            /// Initializes a new instance.
            /// </summary>
            /// <param name="container"></param>
            /// <param name="sync"></param>
            /// <param name="bus"></param>
            /// <param name="host"></param>
            public Instance(CompositionContainer container, AsyncContextThread sync, RnetBus bus, RnetHost host)
            {
                Contract.Requires<ArgumentNullException>(container != null);
                Contract.Requires<ArgumentNullException>(sync != null);
                Contract.Requires<ArgumentNullException>(bus != null);
                Contract.Requires<ArgumentNullException>(host != null);

                Container = container;
                Context = sync;
                Bus = bus;
                Host = host;
            }
 public void AsyncDelegate_ResumesOnSameThread()
 {
     AsyncContext.Run(async () =>
     {
         var thread = new AsyncContextThread();
         int contextThread = -1, resumeThread = -1;
         await thread.Factory.Run(async () =>
         {
             contextThread = Thread.CurrentThread.ManagedThreadId;
             await Task.Yield();
             resumeThread = Thread.CurrentThread.ManagedThreadId;
         });
         Assert.AreEqual(contextThread, resumeThread);
         await thread.JoinAsync();
     });
 }
예제 #8
0
 public void SynchronizationContext_Send_ExecutesInlineIfNecessary()
 {
     Test.Async(async () =>
     {
         using (var thread = new AsyncContextThread())
         {
             int value = 0;
             await thread.Factory.Run(() =>
             {
                 SynchronizationContext.Current.Send(_ => { value = 13; }, null);
                 Assert.AreEqual(13, value);
             });
             Assert.AreEqual(13, value);
         }
     });
 }
예제 #9
0
 public void SynchronizationContext_Send_ExecutesSynchronously()
 {
     Test.Async(async () =>
     {
         using (var thread = new AsyncContextThread())
         {
             var synchronizationContext = await thread.Factory.Run(() => SynchronizationContext.Current);
             int value = 0;
             synchronizationContext.Send(_ => { value = 13; }, null);
             Assert.AreEqual(13, value);
         }
     });
 }
예제 #10
0
 public DebugView(AsyncContextThread thread)
 {
     _thread = thread;
 }
예제 #11
0
 public DebugView(AsyncContextThread thread)
 {
     _thread = thread;
 }
예제 #12
0
 public Bot()
 {
     api = new Api(Settings.Default.TelegramApiKey);
     cancellationTokenSource = new CancellationTokenSource();
     asyncContextThread = new AsyncContextThread();
 }