I have a form that starts a thread. Now I want the form to close automatically when this thread completes.
The only solution I have found so far is to add a timer to the form and check if the stream is alive on each tick. But I want to know if there is a better way to do this?
Currently, my code looks more or less like
partial class SyncForm : Form {
Thread tr;
public SyncForm()
{
InitializeComponent();
}
void SyncForm_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(Synchronize));
thread.IsBackground = true;
thread.Start();
threadTimer.Start();
}
void threadTimer_Tick(object sender, EventArgs e)
{
if (!thread.IsAlive)
{
Close();
}
}
void Synchronize()
{
}
}
source
share