(急)结束一个线程再重新new初始化该线程,新线程跑起来很慢,阻滞(线程里有Lock块)?
这是一个和硬件打交道的线程,通过USB转CAN总线连接电机,通过调用电机厂商提供的API控制电机,因为还需要实时得到电机的信息,所以还开了个线程,为了防止同时访问总线,造成总线冲突,所以加了Lock同步块,具体是通过操作杆去控制,连接操作杆的是一个USB转232的串口,因为串口会重新插拔或松脱之类,所以做了个按钮,相当于重新初始化:过程就是1.彻底停止操作杆控制线程,2.关闭操作杆串口,3.关闭电机连接,4.重新打开操作杆串口,5.重新打开电机连接,6.初始化操作杆控制线程。
彻底停止操作杆线程的语句:
if (threadJoyStickMaxon != null && threadJoyStickMaxon.IsAlive)
{
joyStickLeftPortKeepReading = false;
threadJoyStickMaxon.Join();
threadJoyStickMaxon = null;
}
void CreateManageThreadDeviceInit()
{
threadDeviceInit = new Thread(ManageThreadDeviceInit);
threadDeviceInit.Start();
}
void ManageThreadDeviceInit()
{
OpenJoystickerLeftport();
InitMaxon();
joyStickLeftPortKeepReading = true;
CreateManageThreadJoystickMaxon();
CreateManageThreadReadMaxonStatus(); //打开读电机状态线程
deviceInitThreadResetEvent.WaitOne(); //**MJ:让初始化硬件线程阻塞,直到关闭软件的时候在唤醒它,关闭Maxon电机连接
try //关闭电机连接,电机API提供的方式
{
maxonErrorPromptMessage = "CloseDevice"; //**MJ:如果没有成功打开Maxon电机,关闭Maxon电机连接时或者不关,都会有VC++ RunTime Library Error,定时器初始化设备有时也会,线程初始化设备是每次都有
if (EposCmd.Net.VcsWrapper.Device.VcsCloseDevice(keyHandle, ref errorCode) == 0)
{
ShowErrorInformation(errorCode, maxonErrorPromptMessage);
}
EposCmd.Net.VcsWrapper.Device.Cleanup();
}
catch (Exception ex)
{
writeExceptionToLogFile("MaxonCloseDevice", ex.Message);
}
}
private int InitMaxon() //初始化电机,电机API提供的方式
{
EposCmd.Net.VcsWrapper.Device.Init();
string deviceName = "EPOS2";
string protocolStackName = "CANopen";
int startOfSelection = 1; //1:Get first selection string,0:Get next selection string
string interfaceNameSel="";
int endOfSelection=0;
if(EposCmd.Net.VcsWrapper.Device.VcsGetInterfaceNameSelection(deviceName, protocolStackName, startOfSelection, ref interfaceNameSel, ref endOfSelection, ref errorCode)==0)
{
maxonErrorPromptMessage = "InitMaxon";
ShowErrorInformation(errorCode, maxonErrorPromptMessage);
return 0;
}
keyHandle = EposCmd.Net.VcsWrapper.Device.VcsOpenDevice("EPOS2", "CANopen", interfaceNameSel, "CAN0", ref errorCode);
if (keyHandle == 0)
{
maxonErrorPromptMessage = "InitMaxon";
ShowErrorInformation(errorCode, maxonErrorPromptMessage);
return 0;
}
if (EposCmd.Net.VcsWrapper.Device.VcsSetProtocolStackSettings(keyHandle, maxonBaudrate, maxonTimeout, ref errorCode) == 0) //1 if successful, 0 otherwise
{
maxonErrorPromptMessage = "InitMaxon";
ShowErrorInformation(errorCode, maxonErrorPromptMessage);
return 0;
}
return 1;
}
void CreateManageThreadCloseDevice()
{
threadCloseDevice = new Thread(new ThreadStart(ManageThreadCloseDevice));
//threadCloseDevice.IsBackground = true;
threadCloseDevice.Start();
}
void ManageThreadCloseDevice()
{
CloseAllDevice();
}
void CloseAllDevice()
{
int i = 0;
#region 关闭左操作杆线程和左操作杆串口
if (threadJoyStickMaxon != null && threadJoyStickMaxon.IsAlive)
{
joyStickLeftPortKeepReading = false; //左操作杆控制线程
threadJoyStickMaxon.Join();
threadJoyStickMaxon = null;
}
CloseJoystickerLeftport();
#endregion
if (threadReadMaxonStatus != null && threadReadMaxonStatus.IsAlive) //关闭读电机线程
{
readMaxonStatusFlag = false;
threadReadMaxonStatus.Join(); //**MJ:因为点击关闭连接按钮,GetPulsePositionIs会有错误(线程还没有完全退出时,已关闭电机连接)
threadReadMaxonStatus = null;
}
deviceInitThreadResetEvent.Set();
}