今天下午遇到了一个小问题。多线程中定义了一个全局的Array,需要共享使用,但是遇到了传参数的问题,记录下来。后来发现不需要线程间共享了,但是还是留着以备不时之需。
//定义两个线程
private Thread HsvRsvMsgThread;
private Thread HsvAdsResThread;
MessageQueue queue = new MessageQueue(@".\private$\hrsReservation");
int resortNumber = DataProcess.ResortNumber();
protected override void OnStart(string[] args)
{
queue.Formatter = new XmlMessageFormatter();
Array hotelCodeArray = Array.CreateInstance(typeof(string), resortNumber);
hotelCodeArray = DataProcess.FillHotelCodeArray(resortNumber);
//开始是注释中这么写的,后来编译报错
//HsvRsvMsgThread = new Thread(new ThreadStart(GetHsvRsvMsg));
//HsvRsvMsgThread.Start();
//一老鸟指点后改为(下面方法中传参一个数的话需要这么修改,多个就不知道会什么样呢)
ParameterizedThreadStart pts = new ParameterizedThreadStart(GetHsvRsvMsg);
HsvRsvMsgThread = new Thread(pts);
HsvRsvMsgThread.Start(hotelCodeArray);
HsvAdsResThread = new Thread(new ThreadStart(SendAdsResMsg));
HsvAdsResThread.Start();
}
private void GetHsvRsvMsg(object o)
//private void GetHsvRsvMsg(Array arr) 这种方法需要传对象,然后再转换,好像是delegate本身就是个对象
{
Array arr = o as Array;
//.................
}
private Thread HsvRsvMsgThread;
private Thread HsvAdsResThread;
MessageQueue queue = new MessageQueue(@".\private$\hrsReservation");
int resortNumber = DataProcess.ResortNumber();
protected override void OnStart(string[] args)
{
queue.Formatter = new XmlMessageFormatter();
Array hotelCodeArray = Array.CreateInstance(typeof(string), resortNumber);
hotelCodeArray = DataProcess.FillHotelCodeArray(resortNumber);
//开始是注释中这么写的,后来编译报错
//HsvRsvMsgThread = new Thread(new ThreadStart(GetHsvRsvMsg));
//HsvRsvMsgThread.Start();
//一老鸟指点后改为(下面方法中传参一个数的话需要这么修改,多个就不知道会什么样呢)
ParameterizedThreadStart pts = new ParameterizedThreadStart(GetHsvRsvMsg);
HsvRsvMsgThread = new Thread(pts);
HsvRsvMsgThread.Start(hotelCodeArray);
HsvAdsResThread = new Thread(new ThreadStart(SendAdsResMsg));
HsvAdsResThread.Start();
}
private void GetHsvRsvMsg(object o)
//private void GetHsvRsvMsg(Array arr) 这种方法需要传对象,然后再转换,好像是delegate本身就是个对象
{
Array arr = o as Array;
//.................
}