string temp = this.comboBox2.Text.ToString(); ParameterizedThreadStart ParStart = new System.Threading.ParameterizedThreadStart(ChkFileUpdate); Thread myThread = new Thread(ParStart); object IPAddressSel2 = temp; myThread.Start(IPAddressSel2); 然后download try { CheckLocalFileVersion chk_local_file = new CheckLocalFileVersion(); string[,] files = chk_local_file.GetLocalAllExtFile(strPath, "dll,exe"); mainSocket.OnSend(MainAPI.ChkFileUpdateResult(files)); while (true) { if (this.mainSocket.hash_msg.ContainsKey(TaskService.SYSTEM_UPDATE_CHECK_GET)) { /*返回信息并移出,以便下次接到到相同信息时缓存*/ updateResult = (CustomData[,])this.mainSocket.hash_msg[TaskService.SYSTEM_UPDATE_CHECK_GET]; this.mainSocket.hash_msg.Remove(TaskService.SYSTEM_UPDATE_CHECK_GET); break; } else { Thread.Sleep(1); } } if (updateResult[0, 0].Field == DataField.MESSAGE) { HideLoading(); //没有文件需下载 //Invoke(new EventHandler(delegate(object sender, EventArgs e) // { // this.Hide(); // Form1 main = new Form1(mainSocket,strPath, iVerifyIndexID,iServerGroup); // main.Show(); // } // ) // ); } else { //Thread thDown = new Thread(new ThreadStart(DownloadFile)); //thDown.Start(); //string temp = this.comboBox2.Text.ToString(); ParameterizedThreadStart ParStart = new System.Threading.ParameterizedThreadStart(DownloadFile); Thread myThread = new Thread(ParStart); object IPAddressSel = IPAddressSel1.ToString(); myThread.Start(IPAddressSel); //this.Invoke(new DownloadFile(DownloadFile), obj); } } catch (Exception ex) { MessageBox.Show(ex.Message); ApplicationExit.Exit(ex.Message); } 最后比较字节流下载文件 try { mainSocket.OnDisReceive(); //UpdateFile uSocket = new UpdateFile(); //从登陆窗口获得ip //string IPAddressSel = this.comboBox2.Text.ToString(); UpdateFile uSocket = new UpdateFile(IPAddressSel1.ToString()); #region invoke Invoke(new EventHandler(delegate(object sender, EventArgs e) { this.loading_control1.IsShowBar = true; } ) ); #endregion #region 下载 CustomData[,] __update_file_msg = updateResult; for (int i = 0; i < __update_file_msg.GetLength(0); i++) { string __curr_file_name = __update_file_msg[i, 0].Content.ToString(); int __curr_file_size = int.Parse(__update_file_msg[i, 1].Content.ToString()); int __total_download_size = 0; int __need_download_file_size = 0; #region invoke Invoke(new EventHandler(delegate(object sender, EventArgs e) { this.loading_control1.CueInfo = "正在下载文件" + __curr_file_name + "..."; this.loading_control1.IsShowImg = false; this.loading_control1.IsShowButton = false; this.loading_control1.IsEnableButton = false; this.loading_control1.BarMaxValue = __curr_file_size; } ) ); #endregion #region 下载单一文件 uSocket.SetBufferSize = __curr_file_size; while (__total_download_size < __curr_file_size) { #region 获取需要下载的文件长度 if (__curr_file_size < BUFFER_SIZE) __need_download_file_size = __curr_file_size; else { if (__curr_file_size - __total_download_size > BUFFER_SIZE) __need_download_file_size = BUFFER_SIZE; else __need_download_file_size = __curr_file_size - __total_download_size; } #endregion uSocket.OnSend(MainAPI.GetDownLoadFileByte(__curr_file_name, __need_download_file_size, __total_download_size)); while (true) { if (uSocket.IntiReceivedSize != __total_download_size) { __total_download_size = uSocket.IntiReceivedSize; break; } else Thread.Sleep(1); } Invoke(new EventHandler(delegate(object sender, EventArgs e) { this.loading_control1.BarCurrValue = __total_download_size; } ) ); } #endregion #region 写文件 FileStream fs = new FileStream(strPath + "//" + __curr_file_name + ".xxh", FileMode.Create, FileAccess.Write, FileShare.None, 1024); fs.Write(uSocket.ReceivedBuffer, 0, uSocket.IntiReceivedSize); fs.Close(); #endregion //将已经下载的二进制量重新归0,等待下次下载 uSocket.IntiReceivedSize = 0; } #endregion } catch (Exception ex) { //MessageBox.Show(ex.Message); MessageBox.Show("下载失败,请重新登陆下载"); #region 重新启动 拷贝下载的文件 string lApplicationName = strPath + @"/SM.exe"; StringBuilder lpApplicationName = new StringBuilder(1000); lpApplicationName.Append(lApplicationName); STARTUPINFO sInfo = new STARTUPINFO(); PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); if (!CreateProcess(lpApplicationName, null, null, null, false, 0, null, null, ref sInfo, ref pInfo)) { MessageBox.Show("调用程序" + lApplicationName + "失败"); } //WaitForSingleObject(pInfo.hProcess, 600000000); CloseHandle(pInfo.hProcess); CloseHandle(pInfo.hThread); //Application.Exit(); Process.GetCurrentProcess().Kill(); #endregion //Application.Exit(); Process.GetCurrentProcess().Kill(); //Strategy.ApplicationExit.Exit(ex.Message); } finally { #region 重新启动 拷贝下载的文件 string lApplicationName = strPath + @"/SM.exe"; StringBuilder lpApplicationName = new StringBuilder(1000); lpApplicationName.Append(lApplicationName); STARTUPINFO sInfo = new STARTUPINFO(); PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); if (!CreateProcess(lpApplicationName, null, null, null, false, 0, null, null, ref sInfo, ref pInfo)) { MessageBox.Show("调用程序" + lApplicationName + "失败"); } //WaitForSingleObject(pInfo.hProcess, 600000000); CloseHandle(pInfo.hProcess); CloseHandle(pInfo.hThread); //Application.Exit(); Process.GetCurrentProcess().Kill(); #endregion }