I have a Windows Form as listed below. It has multiple background threads, STA
, etc… I have a function named MyFinalPiece()
. I need to join all threads that are associated with the form before calling this method.
How can I call the Thread.Join here for all threads (irrespective of how many threads are there)?
Note: Even if I add a new thread in future this call should work without break.
CODE
public partial class Form1 : Form
{
int logNumber = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WriteLogFunction("**");
//......Other threads
//..Main thread logic
//All threads should have been completed before this.
MyFinalPiece();
}
private void MyFinalPiece()
{
}
private void WriteLogFunction(string strMessage)
{
string fileName = "MYLog_" + DateTime.Now.ToString("yyyyMMMMdd");
fileName = fileName + ".txt";
using (StreamWriter w = File.AppendText(fileName))
{
w.WriteLine("\r\n{0} ..... {1} + {2}ms >>> {3} ", logNumber.ToString(), DateTime.Now.ToLongTimeString(), DateTime.Now.Millisecond.ToString(), strMessage);
logNumber++;
}
}
}