110,538
社区成员
发帖
与我相关
我的任务
分享
private delegate void FlushClient();//代理
private void button1_Click(object sender, EventArgs e)
{
//对界面上的label的文字进行修改,提示文件开始下载。这里用多线程,是希望刚点【确定】按钮时文字就改变,
//但我从网上找了很多代码,似乎都不行,都是要等到文件下载完毕以后label才会改变
//System.Threading.Thread T = new Thread(CrossThreadFlush);
//T.Start();
FlushClient f = new FlushClient(ThreadFunction);
this.BeginInvoke(f, null);
/*下面是升级的代码 */
//下载升级包
Byte[] b = fc.GetLatestVersion();
string path = @"XLABUpdatePKG.exe";
FileStream fStream = new FileStream(path, FileMode.Create, FileAccess.Write);
fStream.Write(b, 0, b.Length);
fStream.Close();
//运行升级包
System.Diagnostics.Process p=new System.Diagnostics.Process();
p.StartInfo.FileName = @"XLABUpdatePKG.exe ";
p.Start();
p.WaitForExit();
MessageBox.Show("升级完毕");
Program.m.ReleaseMutex();
Program.m.Close();
Application.Restart();
}
private void RunThr()
{
this.Invoke(new MethodInvoker(delegate
{
this.label1.Text = "开始下载";
}));
//开始下载
//do down
this.Invoke(new MethodInvoker(delegate
{
this.label1.Text = "下载完成";
}));
}
private void DoRun()
{
Thread thr = new Thread(new ThreadStart(RunThr));
thr.IsBackground = true;
thr.Start();
}
public void aaa()
{
//下载升级包
Byte[] b = fc.GetLatestVersion();
string path = @"XLABUpdatePKG.exe";
FileStream fStream = new FileStream(path, FileMode.Create, FileAccess.Write);
fStream.Write(b, 0, b.Length);
fStream.Close();
//运行升级包
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"XLABUpdatePKG.exe ";
p.Start();
p.WaitForExit();
}
private void RunTask()
{
aaa();
this.Invoke(new MethodInvoker(delegate
{
label1.Text = "aaaa";
}));
MessageBox.Show("升级完毕");
Program.m.ReleaseMutex();
Program.m.Close();
Application.Restart();
}
private void button1_Click(object sender, EventArgs e)
{
Thread thr = new Thread(new ThreadStart(RunTask));
thr.IsBackground = true;
thr.Start();
}
delegate void RunTaskDelegate();
public void aaa()
{
//下载升级包
Byte[] b = fc.GetLatestVersion();
string path = @"XLABUpdatePKG.exe";
FileStream fStream = new FileStream(path, FileMode.Create, FileAccess.Write);
fStream.Write(b, 0, b.Length);
fStream.Close();
//运行升级包
System.Diagnostics.Process p=new System.Diagnostics.Process();
p.StartInfo.FileName = @"XLABUpdatePKG.exe ";
p.Start();
p.WaitForExit();
}
private void button1_Click(object sender, EventArgs e)
{
RunTaskDelegate runTask = new RunTaskDelegate(aaa);
IAsyncResult asy = runTask.BeginInvoke(null,null);
while (!asy.IsCompleted)
{
label1.Text = "aaaa";
Thread.Sleep(100);
}
runTask.EndInvoke(asy);
MessageBox.Show("升级完毕");
Program.m.ReleaseMutex();
Program.m.Close();
Application.Restart();
}
//newTask就是你的下载线程
NewTaskDelegate task = newTask;
IAsyncResult asyncResult = task.BeginInvoke(200, null, null);
while (!asyncResult.IsCompleted)
{
//在ui中显示你要显示的东西
Thread.Sleep(100);
}
int result = task.EndInvoke(asyncResult);
//做你要在下载完成后处理的东西
private delegate void FlushClient();//代理
private void button1_Click(object sender, EventArgs e)
{
// 这个委托只会讲消息传给ui线程,而不会在线程中对控件进行处理
FlushClient f = new FlushClient(ThreadFunction);
//你可以先把你的主线程命名,然后在你的委托中输出执行委托的线程名字 你就明白了
this.BeginInvoke(f, null);
/*下面是升级的代码 */
// do somthing
//这个做完UI线程空闲了 才回去处理上面异步委托中的操作
}