多线程下的 Lambda表达式 异步 WebClient

线程池

ThreadPool.QueueUserWorkItem(callBack =>
                {
                    XiaoAoHelper.logout();
                });

 

 

解决办法:

1.

直接在窗体的构造函数中加:System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;此时把所有的控件合法性线程检查全部都给禁止掉了。

 

 

 

            Thread t2 = new Thread(() =>
            {
                for (int i = 1; i <= 10; i++)
                {
                    this.BeginInvoke(new Action(() =>
                    {
                        progressBar1.Value = i * 10;
                    }));
                    Thread.Sleep(1000);
                }
            });
            t2.IsBackground = true;
            t2.Start();

 

        private void CheckUpdateVersionAnsyn()
        {
            try
            {
                Uri endpoint = new Uri(string.Format("{0}v.aspx?timer={1}", App.Url, DateTime.Now.ToString()));
                WebClient client = new WebClient();
                var are = new AutoResetEvent(false);

                client.DownloadStringCompleted += (s, e) =>
                {
                    are.Set();

                    if (e.Error == null)
                    {
                        int ver = 0;
                        ver = ExecVaspx(e.Result);
                        base.Dispatcher.BeginInvoke(() =>
                        {
                            if (Application.Current.IsRunningOutOfBrowser)
                            {
                                OOBUpApp(ver);
                            }
                            else
                            {

                                reLogin();

                            }
                        });
                    }
                    else
                    {
                        base.Dispatcher.BeginInvoke(() =>
                        {
                            reLogin();
                            App.ShowMessage("网络连接失败,未获取到新版本!", AletrEnum.Error);
                        });
                    }
                };

                client.DownloadStringAsync(endpoint);

                if (!are.WaitOne(5000))
                {
                    base.Dispatcher.BeginInvoke(() =>
                    {
                        reLogin();
                        App.ShowMessage("网络连接失败,未获取到新版本!", AletrEnum.Error);
                    });
                    client.CancelAsync();
                }
            }
            catch 
            {
                base.Dispatcher.BeginInvoke(() =>
                {
                    reLogin();
                    App.ShowMessage("发生系统性错误,无法更新!", AletrEnum.Error);
                });
            }
        }

 

你可能感兴趣的:(lambda)