thread.suspend,thread.resume方法 [英] thread.suspend, thread.resume methods
问题描述
正在开发c#windows appli,我有文本框,浏览按钮,上传按钮。
使用浏览按钮选择excel文件有7列,然后点击上传按钮。
每个记录发送后的线程必须暂停,直到serialport获得响应数据然后恢复该线程。然后测试响应代码($ machine id(5个字符),响应代码(0/1/2)#)。如果响应代码为0则只发送下一个记录。
这里是代码。
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 Excel = Microsoft.Office.Interop.Excel;
使用 System.IO;
使用 System.Threading;
使用 System.IO.Ports;
使用 System.Text.RegularExpressions;
命名空间 Hand_Held_Data_Transporter
{
public partial class Form1:Form
{
String [] stream_buffer;
字符串 final = null ;
字符串 machId = null ,custId = null ,name = null ,totalBal = null ,paid = null ,lastPaid = null ,due = null ;
byte [] buffer = new byte [ 100 ];
byte btdata;
int ucDataLen;
int ucStart;
int CmdDetected;
int PostCmdStart;
int rspCode;
String s;
string fName = @ C:\\ \\ stL \Download \Download _ + DateTime.Now.ToString( dd MMM yy HH mm )。修剪()+ 。xls;
线程newThread;
线程uploadThread;
ManualResetEvent run = new ManualResetEvent( true );
Excel.Application xlApp = 默认(Excel.Application);
Excel.Workbook xlWorkBook = 默认(Excel.Workbook);
Excel.Worksheet xlWorkSheet = 默认(Excel.Worksheet);
public Form1()
{
InitializeComponent();
progressBar1.Enabled = false ;
progressBar2.Enabled = false ;
}
private void Browse_Click( object sender,EventArgs e)
{
OpenFileDialog fdlg = 新 OpenFileDialog();
fdlg.Filter = 所有文件(*。*)| *。*;
if (fdlg.ShowDialog()== DialogResult.OK)
{
textBox1。 Text = fdlg.FileName;
File.ReadAllText(textBox1.Text);
}
}
public void threadUpload()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0 , true , 5 , , , true ,Excel.XlPlatform.xlWindows , \t, false , false , 0 , true ,< span class =code-digit> 1 , 0 );
Excel._Worksheet xlWorksheet =(Excel._Worksheet)xlWorkbook.Sheets [ 1 ];
Excel.Range xlRange = xlWorksheet.UsedRange;
string []行;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
// progressBar1.Visible = true;
int k = 100 / rowCount;
int i = 2 ;
while ( true )
{
run.WaitOne() ;
if (PostCmdStart == 0 )
{
s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
lines = Regex.Split(s, [$#,]);
rspCode = Convert.ToInt32(行[ 1 ]);
}
如果(rspCode == 0 && ; i < = rowCount)
{
PostCmdStart = 0 ;
for ( int j = 1 ; j < = colCount; j ++)
{
if (j == 1 )
machId = xlRange.Cells [i,j] .Value.ToString();
// upload1 = xlRange.Cells [i,j] .Value .ToString();
if (j == 2 )
custId = xlRange.Cells [i,j] .Value.ToString();
如果(j == 3 )
name = xlRange.Cells [i,j] .Value.ToString();
if (j == 4 )
totalBal = xlRange .Cells [i,j] .Value.ToString();
if (j == 5 )
paid = xlRange .Cells [i,j] .Value.ToString();
if (j == 6 )
lastPaid = xlRange .Cells [i,j] .Value.ToString( dd / MM / yyyy);
// MessageBox.Show(lastpaid:+ lastPaid);
if (j == 7 )
到期= xlRange.Cells [i ,j] .Value.ToString();
final = $ POST, + machId + , + custId + < span class =code-string>, + name + , + totalBal + , + paid + , + lastPaid + , + due + #;
}
if (serialPort1.IsOpen)
{
if (machId.Length < = 5 && custId.Length < = 10 && name.Length < = 15 && totalBal.Length < = 7 && paid.Length < = 7 && lastPaid.Length < = 14 && due.Length < = 7 )
serialPort1.Write(final);
}
progressBar1.Invoke((MethodInvoker)委托
{
if (progressBar1.Value < 100 )
progressBar1.Value + = k;
});
i ++;
}
其他 return ;
// progressBar1.Invoke((MethodInvoker)委托{progressBar1.Value + = 100;});
}
}
私有 void Upload_Click(< span class =code-keyword> object sender,EventArgs e)
{PostCmdStart = 1 ;
rspCode = 0 ;
线程uploadThread = new 线程( new ThreadStart
(threadUpload));
uploadThread.Start();
}
私有 void Form1_Load( object sender,EventArgs e)
{
progressBar1.Visible = false ;
progressBar2.Visible = false ;
serialPort1.Open();
string [] ArrayComPortsNames = null ;
int index = -1;
string ComPortName = null ;
ArrayComPortsNames = SerialPort.GetPortNames();
执行
{
index + = 1 ;
comboBox1.Items.Add(ArrayComPortsNames [index]);
}
while (!((ArrayComPortsNames [index] == ComPortName)
||(index == ArrayComPortsNames.GetUpperBound( 0 ))));
Array.Sort(ArrayComPortsNames);
// 想要先出
if (index == ArrayComPortsNames.GetUpperBound( 0 ))
{
ComPortName = ArrayComPortsNames [ 0 ];
}
comboBox1.Text = ArrayComPortsNames [ 0 ];
}
private void serialPort1_DataReceived( object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
btdata =(字节跨度>)serialPort1.ReadByte();
if (btdata == ' $' )
{
ucStart = 1 ;
ucDataLen = 0 ;
buffer [ucDataLen ++] = btdata;
}
else if ((btdata == ' #')&&(ucStart == 1 ) )
{
ucStart = 0 ;
buffer [ucDataLen] = btdata;
CmdDetected = 1 ;
// OSSemPost(tmSmartCardModule_Sem);
run.Set();
}
else if (ucStart == 1 )
{
buffer [ucDataLen ++] = btdata;
}
其他
{
ucStart = 0 ;
ucDataLen = 0 ;
}
}
它不起作用。只是为了测试我使用另一个appl,响应appli
。
private void serialPort1_DataReceived( object sender,SerialDataReceivedEventArgs e)
{
if (serialPort1.IsOpen)
{
String RecievedData;
RecievedData = serialPort1.ReadExisting();
if (!(RecievedData == ))
{
// textBox2.Text + = RecievedData;
textBox2.Invoke((MethodInvoker) delegate {textBox2.Text + = RecievedData;});
}
Thread.Sleep( 100 );
serialPort1.WriteLine( $ M121,0#);
}
}
机器ID(5个字符),响应代码(0/1/2)#)。如果响应代码为0则只发送下一条记录。
这里是代码。使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 Excel = Microsoft.Office.Interop.Excel;
使用 System.IO;
使用 System.Threading;
使用 System.IO.Ports;
使用 System.Text.RegularExpressions;
命名空间 Hand_Held_Data_Transporter
{
public partial class Form1:Form
{
String [] stream_buffer;
字符串 final = null ;
字符串 machId = null ,custId = null ,name = null ,totalBal = null ,paid = null ,lastPaid = null ,due = null ;
byte [] buffer = new byte [ 100 ];
byte btdata;
int ucDataLen;
int ucStart;
int CmdDetected;
int PostCmdStart;
int rspCode;
String s;
string fName = @ C:\\ \\ stL \Download \Download _ + DateTime.Now.ToString( dd MMM yy HH mm )。修剪()+ 。xls;
线程newThread;
线程uploadThread;
ManualResetEvent run = new ManualResetEvent( true );
Excel.Application xlApp = 默认(Excel.Application);
Excel.Workbook xlWorkBook = 默认(Excel.Workbook);
Excel.Worksheet xlWorkSheet = 默认(Excel.Worksheet);
public Form1()
{
InitializeComponent();
progressBar1.Enabled = false ;
progressBar2.Enabled = false ;
}
private void Browse_Click( object sender,EventArgs e)
{
OpenFileDialog fdlg = 新 OpenFileDialog();
fdlg.Filter = 所有文件(*。*)| *。*;
if (fdlg.ShowDialog()== DialogResult.OK)
{
textBox1。 Text = fdlg.FileName;
File.ReadAllText(textBox1.Text);
}
}
public void threadUpload()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0 , true , 5 , , , true ,Excel.XlPlatform.xlWindows , \t, false , false , 0 , true ,< span class =code-digit> 1 , 0 );
Excel._Worksheet xlWorksheet =(Excel._Worksheet)xlWorkbook.Sheets [ 1 ];
Excel.Range xlRange = xlWorksheet.UsedRange;
string []行;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
// progressBar1.Visible = true;
int k = 100 / rowCount;
int i = 2 ;
while ( true )
{
run.WaitOne() ;
if (PostCmdStart == 0 )
{
s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
lines = Regex.Split(s,
#, ]跨度>);
rspCode = Convert.ToInt32(行[ 1 ]);
}
如果(rspCode == 0 && ; i < = rowCount)
{
PostCmdStart = 0 ;
for ( int j = 1 ; j < = colCount; j ++)
{
if (j == 1 )
machId = xlRange.Cells [i,j] .Value.ToString();
// upload1 = xlRange.Cells [i,j] .Value .ToString();
if (j == 2 )
custId = xlRange.Cells [i,j] .Value.ToString();
如果(j == 3 )
name = xlRange.Cells [i,j] .Value.ToString();
if (j == 4 )
totalBal = xlRange .Cells [i,j] .Value.ToString();
if (j == 5 )
paid = xlRange .Cells [i,j] .Value.ToString();
if (j == 6 )
lastPaid = xlRange .Cells [i,j] .Value.ToString( dd / MM / yyyy);
// MessageBox.Show(lastpaid:+ lastPaid);
if (j == 7 )
到期= xlRange.Cells [i ,j] .Value.ToString();
final =
POST, + machId + , + custId + , + name + , + totalBal + , + paid + , + lastPaid + , + due + #;
}
if (serialPort1.IsOpen)
{
if (machId.Length < = 5 && custId.Length < = 10 && name.Length < = 15 && totalBal.Length < = 7 && paid.Length < = 7 && lastPaid.Length < = 14 && due.Length < = 7 )
serialPort1.Write(final);
}
progressBar1.Invoke((MethodInvoker)委托
{
if (progressBar1.Value < 100 )
progressBar1.Value + = k;
});
i ++;
}
其他 return ;
// progressBar1.Invoke((MethodInvoker)委托{progressBar1.Value + = 100;});
}
}
私有 void Upload_Click(< span class =code-keyword> object sender,EventArgs e)
{PostCmdStart = 1 ;
rspCode = 0 ;
线程uploadThread = new 线程( new ThreadStart
(threadUpload));
uploadThread.Start();
}
私有 void Form1_Load( object sender,EventArgs e)
{
progressBar1.Visible = false ;
progressBar2.Visible = false ;
serialPort1.Open();
string [] ArrayComPortsNames = null ;
int index = -1;
string ComPortName = null ;
ArrayComPortsNames = SerialPort.GetPortNames();
执行
{
index + = 1 ;
comboBox1.Items.Add(ArrayComPortsNames [index]);
}
while (!((ArrayComPortsNames [index] == ComPortName)
||(index == ArrayComPortsNames.GetUpperBound( 0 ))));
Array.Sort(ArrayComPortsNames);
// 想要先出
if (index == ArrayComPortsNames.GetUpperBound( 0 ))
{
ComPortName = ArrayComPortsNames [ 0 ];
}
comboBox1.Text = ArrayComPortsNames [ 0 ];
}
private void serialPort1_DataReceived( object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
btdata =(字节跨度>)serialPort1.ReadByte();
if (btdata == '
hi,
am developing c# windows appli , i have textbox,browse button, upload button.
using browse button select excel file having 7 columns,then click on upload button.
after every record sent thread must be suspended until serialport gets responce data then resume that thread. then test that responce code ($machine id(5 chars),responce code(0/1/2)#).if responce code is 0 then only send the next record.
here is the code .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace Hand_Held_Data_Transporter
{
public partial class Form1 : Form
{
String[] stream_buffer;
String final = null;
String machId = null, custId = null, name = null, totalBal = null, paid = null, lastPaid = null, due = null;
byte[] buffer=new byte[100];
byte btdata;
int ucDataLen;
int ucStart;
int CmdDetected;
int PostCmdStart;
int rspCode;
String s;
string fName = @"C:\STL\Download\Download_" + DateTime.Now.ToString("dd MMM yy HH mm").Trim() + ".xls ";
Thread newThread;
Thread uploadThread;
ManualResetEvent run = new ManualResetEvent(true);
Excel.Application xlApp = default(Excel.Application);
Excel.Workbook xlWorkBook = default(Excel.Workbook);
Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
public Form1()
{
InitializeComponent();
progressBar1.Enabled = false;
progressBar2.Enabled = false;
}
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Filter = "All Files(*.*)|*.*";
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
File.ReadAllText(textBox1.Text);
}
}
public void threadUpload()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
string[] lines;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
// progressBar1.Visible = true;
int k = 100 / rowCount;
int i = 2;
while (true)
{
run.WaitOne();
if (PostCmdStart==0)
{
s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
lines = Regex.Split(s, "[$#,]");
rspCode=Convert.ToInt32(lines[1]);
}
if (rspCode == 0 && i <= rowCount)
{
PostCmdStart = 0;
for (int j = 1; j <= colCount; j++)
{
if (j == 1)
machId = xlRange.Cells[i, j].Value.ToString();
//upload1 = xlRange.Cells[i, j].Value.ToString();
if (j == 2)
custId = xlRange.Cells[i, j].Value.ToString();
if (j == 3)
name = xlRange.Cells[i, j].Value.ToString();
if (j == 4)
totalBal = xlRange.Cells[i, j].Value.ToString();
if (j == 5)
paid = xlRange.Cells[i, j].Value.ToString();
if (j == 6)
lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy");
//MessageBox.Show("lastpaid:"+lastPaid);
if (j == 7)
due = xlRange.Cells[i, j].Value.ToString();
final = "$ POST," + machId + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # ";
}
if (serialPort1.IsOpen)
{
if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7)
serialPort1.Write(final);
}
progressBar1.Invoke((MethodInvoker)delegate
{
if (progressBar1.Value < 100)
progressBar1.Value += k;
});
i++;
}
else return;
// progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value += 100; });
}
}
private void Upload_Click(object sender, EventArgs e)
{ PostCmdStart = 1;
rspCode=0;
Thread uploadThread = new Thread(new ThreadStart
(threadUpload));
uploadThread.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Visible = false;
progressBar2.Visible = false;
serialPort1.Open();
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
comboBox1.Items.Add(ArrayComPortsNames[index]);
}
while (!((ArrayComPortsNames[index] == ComPortName)
|| (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
//want to get first out
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
comboBox1.Text = ArrayComPortsNames[0];
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
btdata = (byte)serialPort1.ReadByte();
if (btdata == '$')
{
ucStart = 1;
ucDataLen = 0;
buffer[ucDataLen++] = btdata;
}
else if ((btdata == '#') && (ucStart==1))
{
ucStart = 0;
buffer[ucDataLen] = btdata;
CmdDetected = 1;
// OSSemPost(tmSmartCardModule_Sem);
run.Set();
}
else if (ucStart==1)
{
buffer[ucDataLen++] = btdata;
}
else
{
ucStart = 0;
ucDataLen = 0;
}
}
it's not working. just for testing am using another appl,responce appli
.
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (serialPort1.IsOpen)
{
String RecievedData;
RecievedData = serialPort1.ReadExisting();
if (!(RecievedData == ""))
{
//textBox2.Text += RecievedData;
textBox2.Invoke((MethodInvoker)delegate { textBox2.Text += RecievedData; });
}
Thread.Sleep(100);
serialPort1.WriteLine("$M121,0#");
}
}
machine id(5 chars),responce code(0/1/2)#).if responce code is 0 then only send the next record.
here is the code .using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Excel=Microsoft.Office.Interop.Excel; using System.IO; using System.Threading; using System.IO.Ports; using System.Text.RegularExpressions; namespace Hand_Held_Data_Transporter { public partial class Form1 : Form { String[] stream_buffer; String final = null; String machId = null, custId = null, name = null, totalBal = null, paid = null, lastPaid = null, due = null; byte[] buffer=new byte[100]; byte btdata; int ucDataLen; int ucStart; int CmdDetected; int PostCmdStart; int rspCode; String s; string fName = @"C:\STL\Download\Download_" + DateTime.Now.ToString("dd MMM yy HH mm").Trim() + ".xls "; Thread newThread; Thread uploadThread; ManualResetEvent run = new ManualResetEvent(true); Excel.Application xlApp = default(Excel.Application); Excel.Workbook xlWorkBook = default(Excel.Workbook); Excel.Worksheet xlWorkSheet = default(Excel.Worksheet); public Form1() { InitializeComponent(); progressBar1.Enabled = false; progressBar2.Enabled = false; } private void Browse_Click(object sender, EventArgs e) { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Filter = "All Files(*.*)|*.*"; if (fdlg.ShowDialog() == DialogResult.OK) { textBox1.Text = fdlg.FileName; File.ReadAllText(textBox1.Text); } } public void threadUpload() { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; string[] lines; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; // progressBar1.Visible = true; int k = 100 / rowCount; int i = 2; while (true) { run.WaitOne(); if (PostCmdStart==0) { s = System.Text.ASCIIEncoding.ASCII.GetString(buffer); lines = Regex.Split(s, "
#,]"); rspCode=Convert.ToInt32(lines[1]); } if (rspCode == 0 && i <= rowCount) { PostCmdStart = 0; for (int j = 1; j <= colCount; j++) { if (j == 1) machId = xlRange.Cells[i, j].Value.ToString(); //upload1 = xlRange.Cells[i, j].Value.ToString(); if (j == 2) custId = xlRange.Cells[i, j].Value.ToString(); if (j == 3) name = xlRange.Cells[i, j].Value.ToString(); if (j == 4) totalBal = xlRange.Cells[i, j].Value.ToString(); if (j == 5) paid = xlRange.Cells[i, j].Value.ToString(); if (j == 6) lastPaid = xlRange.Cells[i, j].Value.ToString("dd/MM/yyyy"); //MessageBox.Show("lastpaid:"+lastPaid); if (j == 7) due = xlRange.Cells[i, j].Value.ToString(); final = "
POST," + machId + "," + custId + "," + name + "," + totalBal + "," + paid + "," + lastPaid + "," + due + " # "; } if (serialPort1.IsOpen) { if (machId.Length <= 5 && custId.Length <= 10 && name.Length <= 15 && totalBal.Length <= 7 && paid.Length <= 7 && lastPaid.Length <= 14 && due.Length <= 7) serialPort1.Write(final); } progressBar1.Invoke((MethodInvoker)delegate { if (progressBar1.Value < 100) progressBar1.Value += k; }); i++; } else return; // progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value += 100; }); } } private void Upload_Click(object sender, EventArgs e) { PostCmdStart = 1; rspCode=0; Thread uploadThread = new Thread(new ThreadStart (threadUpload)); uploadThread.Start(); } private void Form1_Load(object sender, EventArgs e) { progressBar1.Visible = false; progressBar2.Visible = false; serialPort1.Open(); string[] ArrayComPortsNames = null; int index = -1; string ComPortName = null; ArrayComPortsNames = SerialPort.GetPortNames(); do { index += 1; comboBox1.Items.Add(ArrayComPortsNames[index]); } while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0)))); Array.Sort(ArrayComPortsNames); //want to get first out if (index == ArrayComPortsNames.GetUpperBound(0)) { ComPortName = ArrayComPortsNames[0]; } comboBox1.Text = ArrayComPortsNames[0]; } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { btdata = (byte)serialPort1.ReadByte(); if (btdata == '
这篇关于thread.suspend,thread.resume方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!