public void ExecuteTest1()
        {
            var target = new Synchronizer <IEnumerable <double> >();
            Func <int, double> func = a => Math.Sqrt((double)a);
            var callbacker          = new ThreadPoolCallbacker <int, double>(func);
            var arguments           = Enumerable.Range(0, 10);

            foreach (var argument in arguments)
            {
                callbacker.Queue(argument);
            }
            var result = target.Execute(callbacker.EnableCallback)
                         .ToArray();
        }
示例#2
0
        public void QueueTest()
        {
            Action <IEnumerable <double> > callback = delegate(IEnumerable <double> results)
            {
//                MessageBox.Show(String.Join(";", results.Select(a => a.ToString()).ToArray()));
            };
            Func <int, double> func = a => Math.Sqrt((double)a);
            var target    = new ThreadPoolCallbacker <int, double>(func);
            var arguments = Enumerable.Range(0, 10);

            foreach (var argument in arguments)
            {
                target.Queue(argument);
            }
            target.EnableCallback(callback);
//            MessageBox.Show(" ");
        }
        public void ExecuteTest2()
        {
            var urls = new string[]
            {
                "http://www.google.com.br/",
                "http://www.microsoft.com/"
            };

            urls = urls.Concat(urls).Concat(urls).Concat(urls).Concat(urls).Concat(urls).Concat(urls).Concat(urls).ToArray();
            var asyncCallbackSynchronizer = new AsyncCallbackSynchronizer <string>();
            Func <AsyncCallback, string, IAsyncResult> begin = delegate(AsyncCallback callback, string url)
            {
                var request = HttpWebRequest.Create(url);
                return(request.BeginGetResponse(callback, request));
            };
            var synchronizer = new Synchronizer <IEnumerable <IAsyncResult> >();
            var callbacker   = new ThreadPoolCallbacker <SynchronizationDetails <string>, IAsyncResult>(SynchronizationDetails <string> .Execute);

            foreach (var url in urls)
            {
                var synchronizationDetails = new SynchronizationDetails <string>
                {
                    Argument = url,
                    Begin    = begin,
                    AsyncCallbackSynchronizer = asyncCallbackSynchronizer
                };
                callbacker.Queue(synchronizationDetails);
            }
            var results = synchronizer.Execute(callbacker.EnableCallback);
            var reals   = new List <string>();

            foreach (var result in results)
            {
                using (var response = ((WebRequest)result.AsyncState).EndGetResponse(result))
                {
                    reals.Add(response.ResponseUri.AbsoluteUri);
                }
            }
        }