Form not crashing when closed

  • Thread starter Thread starter tendaimare
  • Start date Start date
T

tendaimare

Guest
I have a winforms application that I am working on. This works fine on my development machine but when I install it on another machine It doesnt close the forms properly when i press the close button . This problem only happens when the com port is getting readings from the scale - if I disconnect the scale from the com port the program closes the form with no problems. I am trying to find out what could be the problem - The app uses the SerialPort class to read scale readings and has Threads involved in it.


public partial class Form1 : Form
{
SerialPort portA = new SerialPort();
SerialPort portB = new SerialPort();

Thread ScaleAThread, ScaleBThread;

Stopwatch timeOutWatchA = new Stopwatch();
Stopwatch timeOutWatchB = new Stopwatch();
bool isRunning = false;

public Form1()
{
InitializeComponent();



}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += FormMain_FormClosing;

Settings.LoadSettings();

isRunning = true;

ScaleAThread = new Thread(WeightReadingThreadA);
ScaleBThread = new Thread(WeightReadingThreadB);

ScaleAThread.Start();
ScaleBThread.Start();
}
#region Scale section

private void WeightReadingThreadA()
{
if (string.IsNullOrWhiteSpace(Settings.CurrentSettings.SerialPortA))
{
if (chkScale1.Checked == false)
{
LiveWeightA.Text = "Err";
UnitALbl.Text = "";
lbl_Scale1.Text = "Err";
}
return;
}
portA = new SerialPort
{
PortName = Settings.CurrentSettings.SerialPortA,
BaudRate = Settings.CurrentSettings.BaudRateA,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One
};

TimeOutA();
timeOutWatchA.Start();

try
{
portA.Open();
if (portA.IsOpen)
{
List<byte> dataBytes = new List<byte>();

while (isRunning)
{
byte byt = 0x00;


while (true)
{
byt = (byte)portA.ReadByte();
if (byt == 0x02)
break;

}

dataBytes.Clear();

for (int i = 0; i < 16; i++)
dataBytes.Add((byte)portA.ReadByte());

string hexStr = ByteArrayToHexString(dataBytes.ToArray());

string weightStrHex = hexStr.Substring(4, 12);
string weightStr = "";
for (int i = 0; i < weightStrHex.Length; i++)
{
if (i % 2 != 0)
weightStr += weightStrHex;
}

string unitStr = hexStr.Substring(18, 4);
unitStr = Encoding.ASCII.GetString(HexStringToByteArray(unitStr));

this.Invoke(new Action(() =>
{
try
{
LiveWeightA.Text = Convert.ToInt32(weightStr).ToString();
UnitALbl.Text = unitStr;
lbl_Scale1.Text = Convert.ToInt32(weightStr).ToString();
}
catch (Exception)
{
}
}));

timeOutWatchA.Reset();
timeOutWatchA.Start();
}
}
}
catch (Exception)// ex)
{
//MessageBox.Show(ex.Message);
}
}
//****************************************************//
private void WeightReadingThreadB()
{
if (string.IsNullOrWhiteSpace(Settings.CurrentSettings.SerialPortB))
{
if (chkScale2.Checked == false)
{
LiveWeightB.Text = "Err";
UnitBLbl.Text = "";
lbl_Scale2.Text = "Err";
}
return;
}
portB = new SerialPort
{
PortName = Settings.CurrentSettings.SerialPortB,
BaudRate = Settings.CurrentSettings.BaudRateB,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One
};

TimeOutB();
timeOutWatchB.Start();
try
{
portB.Open();
if (portB.IsOpen)
{
List<byte> dataBytes = new List<byte>();

while (isRunning)
{
byte byt = 0x00;

while (true)
{
byt = (byte)portB.ReadByte();
if (byt == 0x02)
break;
}

dataBytes = new List<byte>();

for (int i = 0; i < 16; i++)
dataBytes.Add((byte)portB.ReadByte());

string hexStr = ByteArrayToHexString(dataBytes.ToArray());

string weightStrHex = hexStr.Substring(4, 12);
string weightStr = "";
for (int i = 0; i < weightStrHex.Length; i++)
{
if (i % 2 != 0)
weightStr += weightStrHex;
}

string unitStr = hexStr.Substring(18, 4);
unitStr = Encoding.ASCII.GetString(HexStringToByteArray(unitStr));

this.Invoke(new Action(() =>
{
try
{
LiveWeightB.Text = Convert.ToInt32(weightStr).ToString();
UnitBLbl.Text = unitStr;
lbl_Scale2.Text = Convert.ToInt32(weightStr).ToString();

}
catch (Exception)
{
}
}));
timeOutWatchB.Reset();
timeOutWatchB.Start();
}
}
}
catch (Exception)// ex)
{
//MessageBox.Show(ex.Message);
}
}
//****************************************************//
private void TimeOutA()
{
Task.Factory.StartNew(() =>
{
while (isRunning)
{
Thread.Sleep(1000);
if ((timeOutWatchA.ElapsedMilliseconds / 1000) > 3)
{
try
{
this.Invoke(new Action(() =>
{
if (chkScale1.Checked == false)
{
//LiveWeightA.Text = "Err";
//UnitALbl.Text = "";
//lbl_Scale1.Text = "Err";
}

}));
}
catch (Exception)
{
}
}
}
});
}
//****************************************************//
private void TimeOutB()
{
Task.Factory.StartNew(() =>
{
while (isRunning)
{
Thread.Sleep(1000);
if ((timeOutWatchB.ElapsedMilliseconds / 1000) > 3)
{
try
{
this.Invoke(new Action(() =>
{
if (chkScale2.Checked == false)
{
LiveWeightB.Text = "Err";
UnitBLbl.Text = "";
lbl_Scale2.Text = "Err";
}

}));
}
catch (Exception)
{
}
}
}
});
}
//****************************************************//
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString().Substring(0, hex.ToString().Length);
}
//****************************************************//
public static byte[] HexStringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
//****************************************************//
public static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
//return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
//****************************************************//
#endregion

private void BtnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are You Sure To Close ?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{


isRunning = false;
ScaleAThread.Abort();
ScaleBThread.Abort();
Thread.Sleep(1000);
this.Close();
}
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
isRunning = false;
ScaleAThread.Abort();
ScaleBThread.Abort();
Thread.Sleep(1000);
}
}

When I click the BtnClose button it just crashes and it gives no error message
If you think it you can achieve it

Continue reading...
 
Back
Top