public TryOperationResult <object> SendWork(
     CancellationToken token,
     Func <object> callback)
 {
     return(SendWork(token,
                     (s, f) => TryOperationResult <object> .Run(callback)));
 }
        /// <summary>
        ///     Post work for to be executed on another thread without regard to when it returns
        /// </summary>
        /// <param name="token"></param>
        /// <param name="callback"></param>
        /// <returns>An operation result that can be waited on</returns>
        public TryOperationResult <object> PostWork(CancellationToken token, DispatchedWork callback)
        {
            var result = new TryOperationResult <object>();

            _workers.Enqueue((source, token2) => {
                try {
                    result.SetSuccess(ExecuteCallback(token, callback, source, token2).Result);
                }
                catch (Exception ex) {
                    result.SetFailure(ex);
                }
                return(result);
            });
            return(result);
        }
        public async Task <TryOperationResult <TResult> > RunOperationAsync <TResult>(
            Func <IServiceProvider, TResult> operation)
        {
            InitializeServiceProvider();
            return(await new Task <TryOperationResult <TResult> >(() => {
                var result = new TryOperationResult <TResult>();

                try {
                    result.SetSuccess(operation(Provider));
                }
                catch (Exception ex) {
                    result.SetFailure(ex);
                }

                return result;
            }));
        }
예제 #4
0
        public void TestSink()
        {
            var sink = new ThreadWorkSource();

            Task.Run(() => {
                using (var worker = ThreadWorkSink.CurrentWorker) {
                    worker.RegisterSource(sink);
                    worker.InstallPump(CancellationToken.None);
                }
            });

            sink.Registered.WaitOne();
            sink.PostWork(CancellationToken.None,
                          (s, t) => TryOperationResult <object> .SuccessFrom(5));
            var actual = sink.SendWork(CancellationToken.None,
                                       (s, t) => TryOperationResult <object> .SuccessFrom(5));

            Assert.AreEqual(5, actual.Result);
        }
        public List <Diagnostic> Compile(BuildProject project, out TryOperationResult <Assembly> assembly)
        {
            var ret = new List <Diagnostic>();

            assembly = new TryOperationResult <Assembly>();
            try {
                var assemblyName = project.ProjectName ?? Path.GetRandomFileName();
                var references   = new Dictionary <string, MetadataReference>();
                foreach (var reference in project.References)
                {
                    var asmRef = MetadataReference.CreateFromFile(reference);
                    references[asmRef.FilePath] = asmRef;
                }


                var compilation = CSharpCompilation.Create(
                    assemblyName,
                    project.Files.Select(code => CSharpSyntaxTree.ParseText(code.Content)).ToArray(),
                    references.Values,
                    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                using (var ms = new MemoryStream()) {
                    var result = compilation.Emit(ms);

                    ret.AddRange(result.Diagnostics);
                    if (result.Success)
                    {
                        ms.Position = 0;
                        assembly.SetSuccess(Assembly.Load(ms.ToArray()));
                    }
                }
            }
            catch (Exception ex) {
                assembly.SetFailure(ex);
            }

            return(ret);
        }