コード例 #1
0
            public void Download(String url, String text, Action <System.Net.DownloadStringCompletedEventArgs, Form, T> completedFct)
            {
                mUrl          = url;
                mText         = text;
                mCompletedFct = completedFct;

                mWebClient = new System.Net.WebClient();
                mWebClient.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
                mWebClient.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                System.Uri uri;
                bool       result = System.Uri.TryCreate(url, UriKind.Absolute, out uri);

                if (!result)
                {
                    MessageBox.Show(mParent, "Internal error: url \"" + url + "\" is invalid.", StringResources.Ares, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // use bg thread because first DwnloadStringAsync call is slow due to proxy detection
                    if (mShowDialog)
                    {
                        mMonitor = new ProgressMonitor(mParent, text);
                    }
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((object o) =>
                    {
                        try
                        {
                            mWebClient.Proxy = System.Net.WebRequest.GetSystemWebProxy();
                            mWebClient.DownloadStringAsync((Uri)o);
                        }
                        catch (Exception)     // On some computers, .NET throws a NullReferenceException in GetSystemWebProxy()
                        {
                            if (mMonitor != null)
                            {
                                mMonitor.Close(new Action(() => { }));
                            }
                        }
                    }), uri);
                }
            }