本文介绍了更好的方法来阻止线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 正在开发Windows应用程序。它通过serialport读取数据并放入excel表格。 完成后该线程应被杀死/停止。 这里是代码:按钮点击。 private void Download_Click( object sender,EventArgs e) { textBox2.Text = fName; p = 2 ; fName = @ C:\STL \Download \Download _ + DateTime .Now.ToString( dd MMM yy HH mm)。修剪()+ 。xls; progressBar2.Visible = true ; serialPort1.WriteLine( $ GET, + textBox3.Text。 ToString()+ ,0,#); 线程downloadThread = new 线程( new ThreadStart(threadDownload)); downloadThread.Start(); } 和线程方法是: public void threadDownload() { 尝试 { Excel.Application xlApp = 默认(Excel.Application); Excel.Workbook xlWorkBook = 默认(Excel.Workbook); Excel.Worksheet xlWorkSheet = 默认(Excel.Worksheet); object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 1 ); xlWorkSheet.Cells [ 1 , 1 ] = 机器ID; xlWorkSheet.Cells [ 1 , 2 ] = 客户ID; xlWorkSheet.Cells [ 1 , 3 ] = 名称; xlWorkSheet.Cells [ 1 , 4 ] = 总余额; xlWorkSheet.Cells [ 1 , 5 ] = 付费金额; xlWorkSheet.Cells [ 1 , 6 ] = 上次付款日期; xlWorkSheet.Cells [ 1 , 7 ] = 到期金额; // String machineID = textBox3.Text.ToString(); if (textBox3.InvokeRequired) { textBox3.Invoke( new MethodInvoker( delegate {machId = textBox3.Text;})); } s = null ; // int drows = 0; xlWorkSheet.Cells [ 1 , 1 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 2 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 3 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 4 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 5 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 6 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 7 ]。Interior.ColorIndex = 39 ; while ( true ) { run .WaitOne(); s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); string [] lines = Regex.Split(s, [$#,]); int resp = Convert.ToInt32(行[ 2 ]); int totalrows = Convert.ToInt32(行[ 4 ]); int m = 100 / totalrows; if (resp == 0 && p < = totalrows + 1 ) { for ( int q = 1 ; q < = 7 ; q ++) { if (q == 1 )((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 1 ]; if (q == 2 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 5 ]; if (q == 3 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 6 ]; if (q == 4 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 7 ]; if (q == 5 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 8 ]; if (q == 6 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 9 ]; if (q == 7 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 10 ]; } progressBar2.Invoke((MethodInvoker)委托 { if (progressBar2.Value < 100 ) progressBar2 .Value + = m; }); downrows = p - 1 ; serialPort1.WriteLine( $ GET, + machId + , + downrows + ,#); p ++; } else { if (MessageBox .Show( 交易已停止, 通知,MessageBoxButtons.OK)== DialogResult.OK) { progressBar2.Invoke((MethodInvoker) 委托 { progressBar2.Value = 0 ; }); return ; } } 如果(行[ 3 ]。等于(行[ 4 ])) { serialPort1.WriteLine( $ GET, + machId + ,65535,#); progressBar2.Invoke((MethodInvoker)委托 { progressBar2.Value = 100 ; }); break ; } else if (p > totalrows + 1 ) break ; // drows ++; } xlWorkBook.SaveAs(fName,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue); xlWorkBook.Close( true ,misValue,misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } // 尝试 catch (例外p) { MessageBox.Show(p.StackTrace); } 最后 { 如果(xlApp!= null ) releaseObject(xlApp); if (xlWorkBook!= null ) releaseObject(xlWorkBook); if (xlWorkSheet!= null ) releaseObject(xlWorkSheet); } if (System.IO.File.Exists(fName)) { if (MessageBox.Show( 你想打开excel文件吗? , this .Text,MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.Yes) { try { System.Diagnostics.Process.Start(fName); } catch (Exception ex) { MessageBox.Show( 打开excel文件时出错。 + Environment.NewLine + ex.Message,这个 .Text,MessageBoxButtons.OK,MessageBoxIcon.Error); } } } } 解决方案 GET, + textBox3.Text.ToString()+ ,0,#); 线程downloadThread = new 线程( new ThreadStart(threadDownload)); downloadThread.Start(); } 和线程方法是: public void threadDownload() { try { Excel.Application xlApp = default (Excel.Application); Excel.Workbook xlWorkBook = default (Excel.Workbook); Excel.Worksheet xlWorkSheet = 默认(Excel.Worksheet); object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item( 1 ); xlWorkSheet.Cells [ 1 , 1 ] = 机器ID; xlWorkSheet.Cells [ 1 , 2 ] = 客户ID; xlWorkSheet.Cells [ 1 , 3 ] = 名称; xlWorkSheet.Cells [ 1 , 4 ] = 总余额; xlWorkSheet.Cells [ 1 , 5 ] = 付费金额; xlWorkSheet.Cells [ 1 , 6 ] = 上次付款日期; xlWorkSheet.Cells [ 1 , 7 ] = 到期金额; // String machineID = textBox3.Text.ToString(); if (textBox3.InvokeRequired) { textBox3.Invoke( new MethodInvoker( delegate {machId = textBox3.Text;})); } s = null ; // int drows = 0; xlWorkSheet.Cells [ 1 , 1 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 2 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 3 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 4 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 5 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 6 ]。Interior.ColorIndex = 39 ; xlWorkSheet.Cells [ 1 , 7 ]。Interior.ColorIndex = 39 ; while ( true ) { run .WaitOne(); s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); string [] lines = Regex.Split(s, #,]); int resp = Convert.ToInt32(行[ 2 ]); int totalrows = Convert.ToInt32(行[ 4 ]); int m = 100 / totalrows; if (resp == 0 && p < = totalrows + 1 ) { for ( int q = 1 ; q < = 7 ; q ++) { if (q == 1 )((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 1 ]; if (q == 2 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 5 ]; if (q == 3 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 6 ]; if (q == 4 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 7 ]; if (q == 5 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 8 ]; if (q == 6 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 9 ]; if (q == 7 )((Microsoft.Office.Interop .Excel.Range)xlWorkSheet.Cells [p,q])。Value2 = lines [ 10 ]; } progressBar2.Invoke((MethodInvoker)委托 { if (progressBar2.Value < 100 ) progressBar2 .Value + = m; }); downrows = p - 1 ; serialPort1.WriteLine( GET, + machId + , + downrows + ,#); p ++; } else { if (MessageBox .Show( 交易已停止, 通知,MessageBoxButtons.OK)== DialogResult.OK) { progressBar2.Invoke((MethodInvoker) 委托 { progressBar2.Value = 0 ; }); return ; } } 如果(行[ 3 ]。等于(行[ 4 ])) { serialPort1.WriteLine( Hi,am developing windows application. it reads data through serialport and places into excel sheet.after completion that thread should be killed/stopped.here is the code :for button click.private void Download_Click(object sender, EventArgs e) { textBox2.Text = fName; p = 2; fName = @"C:\STL\Download\Download_" + DateTime.Now.ToString("dd MMM yy HH mm").Trim() + ".xls "; progressBar2.Visible = true; serialPort1.WriteLine("$GET,"+textBox3.Text.ToString()+",0,#"); Thread downloadThread = new Thread(new ThreadStart(threadDownload)); downloadThread.Start(); }and thread method is:public void threadDownload(){ try { Excel.Application xlApp = default(Excel.Application); Excel.Workbook xlWorkBook = default(Excel.Workbook); Excel.Worksheet xlWorkSheet = default(Excel.Worksheet); object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "Machine ID"; xlWorkSheet.Cells[1, 2] = "Customer ID"; xlWorkSheet.Cells[1, 3] = "Name"; xlWorkSheet.Cells[1, 4] = "Total Balance"; xlWorkSheet.Cells[1, 5] = "Paid Amount"; xlWorkSheet.Cells[1, 6] = "Last Paid Date"; xlWorkSheet.Cells[1, 7] = "Due Amount"; // String machineID = textBox3.Text.ToString(); if (textBox3.InvokeRequired) { textBox3.Invoke(new MethodInvoker(delegate { machId = textBox3.Text; })); } s = null; // int drows = 0; xlWorkSheet.Cells[1, 1].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 2].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 3].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 4].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 5].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 6].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 7].Interior.ColorIndex = 39; while (true) { run.WaitOne(); s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); string[] lines = Regex.Split(s, "[$#,]"); int resp = Convert.ToInt32(lines[2]); int totalrows = Convert.ToInt32(lines[4]); int m = 100 / totalrows; if (resp == 0 && p <= totalrows + 1) { for (int q = 1; q <= 7; q++) { if (q == 1) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[1]; if (q == 2) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[5]; if (q == 3) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[6]; if (q == 4) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[7]; if (q == 5) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[8]; if (q == 6) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[9]; if (q == 7) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[10]; } progressBar2.Invoke((MethodInvoker)delegate { if (progressBar2.Value < 100) progressBar2.Value += m; }); downrows = p - 1; serialPort1.WriteLine("$GET," + machId + "," + downrows + ",#"); p++; } else { if (MessageBox.Show("Transaction has been stopped", "Notification", MessageBoxButtons.OK) == DialogResult.OK) { progressBar2.Invoke((MethodInvoker)delegate { progressBar2.Value = 0; }); return; } } if (lines[3].Equals(lines[4])) { serialPort1.WriteLine("$GET," + machId + ",65535,#"); progressBar2.Invoke((MethodInvoker)delegate { progressBar2.Value = 100; }); break; } else if (p > totalrows + 1) break; // drows++; } xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); }//try catch (Exception p) { MessageBox.Show(p.StackTrace); } finally { if (xlApp != null) releaseObject(xlApp); if (xlWorkBook != null) releaseObject(xlWorkBook); if (xlWorkSheet != null) releaseObject(xlWorkSheet); } if (System.IO.File.Exists(fName)) { if (MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { System.Diagnostics.Process.Start(fName); } catch (Exception ex) { MessageBox.Show("Error opening the excel file." + Environment.NewLine + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }} 解决方案 GET,"+textBox3.Text.ToString()+",0,#"); Thread downloadThread = new Thread(new ThreadStart(threadDownload)); downloadThread.Start(); }and thread method is:public void threadDownload(){ try { Excel.Application xlApp = default(Excel.Application); Excel.Workbook xlWorkBook = default(Excel.Workbook); Excel.Worksheet xlWorkSheet = default(Excel.Worksheet); object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "Machine ID"; xlWorkSheet.Cells[1, 2] = "Customer ID"; xlWorkSheet.Cells[1, 3] = "Name"; xlWorkSheet.Cells[1, 4] = "Total Balance"; xlWorkSheet.Cells[1, 5] = "Paid Amount"; xlWorkSheet.Cells[1, 6] = "Last Paid Date"; xlWorkSheet.Cells[1, 7] = "Due Amount"; // String machineID = textBox3.Text.ToString(); if (textBox3.InvokeRequired) { textBox3.Invoke(new MethodInvoker(delegate { machId = textBox3.Text; })); } s = null; // int drows = 0; xlWorkSheet.Cells[1, 1].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 2].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 3].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 4].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 5].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 6].Interior.ColorIndex = 39; xlWorkSheet.Cells[1, 7].Interior.ColorIndex = 39; while (true) { run.WaitOne(); s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); string[] lines = Regex.Split(s, "#,]"); int resp = Convert.ToInt32(lines[2]); int totalrows = Convert.ToInt32(lines[4]); int m = 100 / totalrows; if (resp == 0 && p <= totalrows + 1) { for (int q = 1; q <= 7; q++) { if (q == 1) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[1]; if (q == 2) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[5]; if (q == 3) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[6]; if (q == 4) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[7]; if (q == 5) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[8]; if (q == 6) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[9]; if (q == 7) ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p, q]).Value2 = lines[10]; } progressBar2.Invoke((MethodInvoker)delegate { if (progressBar2.Value < 100) progressBar2.Value += m; }); downrows = p - 1; serialPort1.WriteLine("GET," + machId + "," + downrows + ",#"); p++; } else { if (MessageBox.Show("Transaction has been stopped", "Notification", MessageBoxButtons.OK) == DialogResult.OK) { progressBar2.Invoke((MethodInvoker)delegate { progressBar2.Value = 0; }); return; } } if (lines[3].Equals(lines[4])) { serialPort1.WriteLine(" 这篇关于更好的方法来阻止线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!
05-24 08:08