private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// check if worker thread is running
if ((workerThread != null) && (workerThread.IsAlive))
{
needToStop = true;
while (!workerThread.Join(100))
Application.DoEvents();
}
}