示例#1
0
        static async Task <MulticastResults> MulticastWaitResultAsync(AjaxOptions opts, Ajax ajax = null)
        {
            var results = new MulticastResults(ajax, opts);

            foreach (var url in opts.Urls)
            {
                opts.url = url;
                var visit = MakeVisitFromOptions(opts, ajax);
                results.AddVisit(visit);
                var es = await InternalRequestAsync(visit, (ret, v) => {
                    visit.Result = ret;
                    results.AddSuccess(visit);
                }, (err, v) => {
                    visit.Exception = err;
                    results.AddError(visit);
                    return(true);
                });
            }
            if (results.Errors.Count > 0)
            {
                if (opts.error != null)
                {
                    if (!opts.error(results, new Visit(opts, ajax)))
                    {
                        throw results;
                    }
                }
                else
                {
                    throw results;
                }
            }
            else
            {
                if (opts.success != null)
                {
                    opts.success(results, new Visit(opts, ajax));
                }
            }
            return(results);
        }
示例#2
0
        static void MulticastUseAsync(AjaxOptions opts, Ajax ajax)
        {
            var results = new MulticastResults(ajax, opts);
            int count   = 0;
            int total   = opts.Urls.Count();
            Action <string, Visit, MulticastResults> req = (url, visit, rs) => {
                opts.url = url;
                InternalRequestAsync(visit, (ret, v) => {
                    v.Result = ret;
                    results.AddSuccess(v);
                    lock (results) count++;
                    if (count == total)
                    {
                        if (results.Errors.Count > 0)
                        {
                            if (opts.error != null)
                            {
                                if (!opts.error(results, v))
                                {
                                    throw results;
                                }
                            }
                            else
                            {
                                throw results;
                            }
                        }
                        else
                        {
                            if (opts.success != null)
                            {
                                opts.success(results, v);
                            }
                        }
                    }
                }, (err, v) => {
                    visit.Exception = err;
                    results.AddError(visit);
                    lock (results) count++;
                    if (count == total)
                    {
                        if (opts.error != null)
                        {
                            if (!opts.error(results, v))
                            {
                                throw results;
                            }
                        }
                        else
                        {
                            throw results;
                        }
                    }
                    return(true);
                }).Start();
            };

            foreach (var url in opts.Urls)
            {
                opts.url = url;
                var visit = MakeVisitFromOptions(opts, ajax);
                results.AddVisit(visit);
                req(url, visit, results);
            }
        }