using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO.Ports;namespace rating { public partial class DistancePulse : UserControl { double i = 0; string direction = "1"; double stopechain = 0; Boolean count=true; Boolean counttrue = false; int stop = 1; Thread pulsesThread; public SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
public DistancePulse() { InitializeComponent();
} private void btn_play_Click(object sender, EventArgs e) { try { port.Open(); } catch (Exception ex) { } stop = 1; // Create the MSSQL IOCP Thread Pool IOCPThreadPool pThreadPool = new IOCPThreadPool(0, 10, 20, new IOCPThreadPool.USER_FUNCTION(IOCPThreadFunction)); //for(int i =1;i<10000;i++) { pThreadPool.PostEvent(1234); } Thread.Sleep(100); pThreadPool.Dispose(); // pulsesThread = new Thread(new ThreadStart(countPulses)); // pulsesThread.Start();
timer1.Enabled = true; btn_play.Enabled = false; } //******************************************************************** /**/ /// <summary> Function to be called by the IOCP thread pool. Called when /// a command is posted for processing by the SocketManager </summary> /// <param name="iValue"> The value provided by the thread posting the event </param> static public void IOCPThreadFunction(int iValue) {
try { do { count = port.CtsHolding; switch (count) { case true: counttrue = true; break; } if (count == false && counttrue == true) { switch (direction) { case "1": counttrue = false; i++; break; case "2": counttrue = false; i--; break; } } } while (stop == 1); } catch (Exception pException) { Console.WriteLine(pException.Message); } } }还有个问题!!!为什么我在上面明明定义了count,counttrue,stop...等等,在 static public void IOCPThreadFunction(int iValue)却用不了,要求从新定意?????????????????????????
2楼.....给的例子我也看过...看不懂....而且好想编译不了,有没有简单的啊
private unsafe static extern uint32 createiocompletionport(uint32 hfile, uint32 hexistingcompletionport, uint32* puicompletionkey, uint32 uinumberofconcurrentthreads);就是这段错....为什么啊?大虾请教
类似的还有checked,using,.....
你的错误好象是要把工程属性修改下,把允许不安全代码属性变为true
刚才在网上找下完成端口的一些知识,以前是不知道的....
你的错误好象是要把工程属性修改下,把允许不安全代码属性变为true 找不到在哪里改.....
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;namespace rating
{
public partial class DistancePulse : UserControl
{
double i = 0;
string direction = "1";
double stopechain = 0;
Boolean count=true;
Boolean counttrue = false;
int stop = 1;
Thread pulsesThread;
public SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
public DistancePulse()
{
InitializeComponent();
}
private void btn_play_Click(object sender, EventArgs e)
{
try
{ port.Open();
}
catch (Exception ex) { }
stop = 1; // Create the MSSQL IOCP Thread Pool
IOCPThreadPool pThreadPool = new IOCPThreadPool(0, 10, 20, new IOCPThreadPool.USER_FUNCTION(IOCPThreadFunction));
//for(int i =1;i<10000;i++)
{
pThreadPool.PostEvent(1234);
} Thread.Sleep(100); pThreadPool.Dispose(); // pulsesThread = new Thread(new ThreadStart(countPulses));
// pulsesThread.Start();
timer1.Enabled = true;
btn_play.Enabled = false;
} //********************************************************************
/**/
/// <summary> Function to be called by the IOCP thread pool. Called when
/// a command is posted for processing by the SocketManager </summary>
/// <param name="iValue"> The value provided by the thread posting the event </param>
static public void IOCPThreadFunction(int iValue)
{
try
{
do
{
count = port.CtsHolding; switch (count)
{ case true: counttrue = true; break;
} if (count == false && counttrue == true)
{
switch (direction)
{ case "1": counttrue = false; i++; break;
case "2": counttrue = false; i--; break; } }
} while (stop == 1);
} catch (Exception pException)
{
Console.WriteLine(pException.Message);
}
}
}还有个问题!!!为什么我在上面明明定义了count,counttrue,stop...等等,在 static public void IOCPThreadFunction(int iValue)却用不了,要求从新定意?????????????????????????
你定义的这些东西:count,counttrue,stop...等
是变量,需要把类实例化才能使用的,如果不想实例化类,那就把它声明为静态类吧