请教如何在C#的线程中如何使用字节数组?
......
System.Threading.Timer timer = null;
......
timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.timer_Elapsed));
........
void timer_Elapsed(object state)
{
sbyte[] pcKey = (sbyte[])(Array)(System.Text.Encoding.ASCII.GetBytes("192.168.0.1");
myCOM.myFun(ref pcKey[0]); //myFun为我的一个COM组件myCOM中的一个函数,对应c++的char*。
}
.....
如果是在form的定时器中使用,那就没有任何问题。
但是在线程的定时器中使用,传递的参数就会只有第一个字符对,后面的都是乱的数据。
我想是C#的线程中的内存分配方式的问题,请问该如何处理才能如此传递参数?
谢谢 --------------------编程问答-------------------- 不懂!! --------------------编程问答-------------------- --------------------编程问答--------------------
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
// Create an event to signal the timeout count threshold in the
// timer callback.
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create an inferred delegate that invokes methods for the timer.
TimerCallback tcb = statusChecker.CheckStatus;
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
private int invokeCount;
private int maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
参考这范例 --------------------编程问答-------------------- 学习。 --------------------编程问答--------------------
static void Main(string[] args)--------------------编程问答-------------------- 现在我知道了,跨线程需要用委托,我将代码改为
{
//声明线程
Thread thread = new Thread((ParameterizedThreadStart)delegate(object parameter)
{
byte[] data = parameter as byte[];
if (data == null) return;
foreach (byte b in data)
{
Console.Write(b.ToString("X2") + " ");
}
Console.WriteLine("Thread over.");
});
//创建一个数组,作为参数传递过去。传递时会转换为object类型,所以线程方法内需要类型转换
//参数是定义好的,object类型。不能修改,只能转换。
thread.Start(new byte[] { 1, 2, 3, 4, 5, 6 });
//输出结果:
//01 02 03 04 05 06 Thread over.
Console.ReadKey();
}
......
delegate void DeleCloseSocketConn(string ip, int iport);
private void CloseSocketConn(string ip, int iport)
{
sbyte[] xx = (sbyte[])(Array)System.Text.Encoding.ASCII.GetBytes(ip + ":" + iport.ToString());
myCOM.myFun.myFun(ref xx[0]);//myFun为我的一个COM组件myCOM中的一个函数,对应c++的char*。
}
......
System.Threading.Timer timer = null;
......
timer = new System.Threading.Timer(new System.Threading.TimerCallback(this.timer_Elapsed));
........
void timer_Elapsed(object state)
{
DeleCloseSocketConn d = new DeleCloseSocketConn(CloseSocketConn);
d.Invoke("192.168.1.1", 5555);
}
.....
但是仍然无法将参数传过去 --------------------编程问答-------------------- 你试着将pcKey申明为类的成员变量试试
补充:.NET技术 , C#