windows mobile多线程示例
Author:Quietwalk
Date:
private static bool bThreadEnd;
private Thread workThread;
DateTime dtStart;
DateTime dtEnd;
#region download
private void miDownLoad_Click(object sender, EventArgs e)
{
this.btnSend.Visible = false;
this.btnReceiveFile.Visible = true; //使能下载按钮
this.btnReceiveFile.Focus();
this.statusBar1.Text = "";
}
//下载并接收文件
//调用WebService 将应用服务器上的 Zip 包转换成二进制流发送出来
//调用设备本地的代码接收并存放
private void btnReceiveFile_Click(object sender, EventArgs e)
{
//*******************************************
this.timer1.Enabled = true;
this.btnReceiveFile.BackColor = Color.Yellow;
this.statusBar1.Text = "正在下载数据,请稍候。。。";
dtStart = DateTime.Now;
workThread = new Thread(new ThreadStart(ReceiveAndSaveFile));
workThread.Start();
}
private void ReceiveAndSaveFile()
{
bThreadEnd = false;
GPRSTest.jwsHelloWorld.HelloWordImplService sv = new HelloWordImplService();
QuietwalkClassLibrary.CommonFunctions.SaveFile(sv.convertZip2ByteArray());//返回的是执行后成功与否,但这里不需要接收此参数
bThreadEnd = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!bThreadEnd)//如果线程没有结束
{
if (this.progressBar1.Value == progressBar1.Maximum)//到最大了之后再从0开始
{
this.progressBar1.Value = 0;
}
this.progressBar1.Value += 5;
}
else//线程结束了
{
this.timer1.Enabled = false;//关闭计时器
this.progressBar1.Value = this.progressBar1.Maximum;
dtEnd = DateTime.Now;
if (QuietwalkClassLibrary.CommonFunctions.bDoneStatus)
{
this.statusBar1.Text = this.statusBar1.Text = "下载并存储到设备所用时间: " + QuietwalkClassLibrary.CommonFunctions.GetTimeSpan(dtStart, dtEnd).ToString() + "秒--" + DateTime.Now.ToShortTimeString();
}
else
{
this.statusBar1.Text = "下载失败!";
}
}
}
#endregion download