多线程下的 Lambda表达式 异步 WebClient 读取程序图标,来作为托盘 图标 logo ico

时间:2021-01-14 00:09:17
//读取程序图标,来作为托盘图标
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

线程池

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

解决办法:

1.

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

            Thread t2 = new Thread(() =>
{
for (int i = ; i <= ; i++)
{
this.BeginInvoke(new Action(() =>
{
progressBar1.Value = i * ;
}));
Thread.Sleep();
}
});
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 = ;
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())
{
base.Dispatcher.BeginInvoke(() =>
{
reLogin();
App.ShowMessage("网络连接失败,未获取到新版本!", AletrEnum.Error);
});
client.CancelAsync();
}
}
catch
{
base.Dispatcher.BeginInvoke(() =>
{
reLogin();
App.ShowMessage("发生系统性错误,无法更新!", AletrEnum.Error);
});
}
}