장치 연결 로직을 별도 쓰레드로 분리
상태머신에서 장치 연결(AGV, XBee, BMS)이 메인 루프를 블로킹하는 문제 해결 주요 변경사항: - DeviceConnectionWorker: 별도 쓰레드에서 장치 연결 처리 - StartDeviceConnectionThread: 쓰레드 시작 로직 - StopDeviceConnectionThread: 프로그램 종료 시 안전한 쓰레드 종료 - sm_SPS: 연결 로직 제거, 쓰레드 시작만 담당 - __Closing: 프로그램 종료 시 쓰레드 종료 호출 이점: - 장치 연결 중 상태머신 블로킹 방지 - 1초 간격으로 비동기 연결 시도 - 프로그램 종료 시 안전한 쓰레드 정리 파일: - StateMachine/_SPS.cs: 쓰레드 로직 추가 - fMain.cs: 종료 시 쓰레드 정리 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Media.Animation;
|
||||
using AR;
|
||||
using arCtl;
|
||||
@@ -18,6 +19,10 @@ namespace Project
|
||||
DateTime chargesynctime = DateTime.Now;
|
||||
DateTime agvsendstarttime = DateTime.Now;
|
||||
|
||||
// 장치 연결 쓰레드 관련
|
||||
private Thread deviceConnectionThread;
|
||||
private volatile bool isDeviceConnectionRunning = false;
|
||||
|
||||
void ConnectSerialPort(arDev.arRS232 dev, string port, int baud, eVarTime conn, eVarTime conntry, eVarTime recvtime)
|
||||
{
|
||||
if (dev.IsOpen == false && port.isEmpty() == false)
|
||||
@@ -95,11 +100,47 @@ namespace Project
|
||||
}
|
||||
}
|
||||
|
||||
void sm_SPS(object sender, EventArgs e)
|
||||
// 장치 연결 쓰레드 시작
|
||||
private void StartDeviceConnectionThread()
|
||||
{
|
||||
if (PUB.sm.Step < eSMStep.IDLE || PUB.sm.Step >= eSMStep.CLOSING) return;
|
||||
if (deviceConnectionThread == null || !deviceConnectionThread.IsAlive)
|
||||
{
|
||||
isDeviceConnectionRunning = true;
|
||||
deviceConnectionThread = new Thread(DeviceConnectionWorker);
|
||||
deviceConnectionThread.IsBackground = true;
|
||||
deviceConnectionThread.Name = "DeviceConnectionThread";
|
||||
deviceConnectionThread.Start();
|
||||
PUB.log.Add("DeviceConnection", "장치 연결 쓰레드 시작");
|
||||
}
|
||||
}
|
||||
|
||||
// 장치 연결 쓰레드 종료
|
||||
private void StopDeviceConnectionThread()
|
||||
{
|
||||
if (deviceConnectionThread != null && deviceConnectionThread.IsAlive)
|
||||
{
|
||||
isDeviceConnectionRunning = false;
|
||||
if (!deviceConnectionThread.Join(2000)) // 2초 대기
|
||||
{
|
||||
try
|
||||
{
|
||||
deviceConnectionThread.Abort();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
PUB.log.Add("DeviceConnection", "장치 연결 쓰레드 종료");
|
||||
}
|
||||
}
|
||||
|
||||
// 장치 연결 처리 워커 (별도 쓰레드에서 실행)
|
||||
private void DeviceConnectionWorker()
|
||||
{
|
||||
while (isDeviceConnectionRunning)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (PUB.sm.Step >= eSMStep.IDLE && PUB.sm.Step < eSMStep.CLOSING)
|
||||
{
|
||||
//agv connect
|
||||
ConnectSerialPort(PUB.AGV, PUB.setting.Port_AGV, PUB.setting.Baud_AGV,
|
||||
eVarTime.LastConn_AGV, eVarTime.LastConnTry_AGV, eVarTime.LastRecv_AGV);
|
||||
@@ -129,11 +170,30 @@ namespace Project
|
||||
{
|
||||
Console.WriteLine("bms auto disconnect");
|
||||
PUB.BMS.Close();
|
||||
VAR.TIME.Set(eVarTime.LastConn_BAT,DateTime.Now.AddSeconds(5));
|
||||
VAR.TIME.Set(eVarTime.LastConn_BAT, DateTime.Now.AddSeconds(5));
|
||||
}
|
||||
}
|
||||
//ConnectSerialPort(PUB.BMS, PUB.setting.Port_BAT, PUB.setting.Baud_BAT,
|
||||
// eVarTime.LastConn_BAT, eVarTime.LastConnTry_BAT, eVarTime.LastRecv_BAT);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PUB.log.AddE($"DeviceConnection: {ex.Message}");
|
||||
}
|
||||
|
||||
// 1초 대기 후 다시 체크
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void sm_SPS(object sender, EventArgs e)
|
||||
{
|
||||
if (PUB.sm.Step < eSMStep.IDLE || PUB.sm.Step >= eSMStep.CLOSING) return;
|
||||
|
||||
// 장치 연결 쓰레드가 실행 중이 아니면 시작
|
||||
if (!isDeviceConnectionRunning)
|
||||
{
|
||||
StartDeviceConnectionThread();
|
||||
}
|
||||
|
||||
//지그비상태전송
|
||||
if (PUB.XBE != null && PUB.XBE.IsOpen)
|
||||
|
||||
@@ -110,6 +110,9 @@ namespace Project
|
||||
|
||||
private void __Closing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
// 장치 연결 쓰레드 종료
|
||||
StopDeviceConnectionThread();
|
||||
|
||||
PUB.popup.needClose = true;
|
||||
if (remoteClose == true)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user