Here is my code. The log file which is created in the startLog method has proper count from 1 to 1000 but log file being created by log method has duplicate values. For example if 88 is repeated then 89 is missed and next number printed is 90. Can any one please explain why is this happening ?
namespace TestThreading
{
public partial class Form1 : Form
{
NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
new Thread(startLog).Start();
}
private void startLog()
{
int i = 0;
string str;
while (i < 1000)
{
str = (++i).ToString();
logger.Trace(str);
Thread t = new Thread(() => log(new TestObject(str)));
t.Start();
}
MessageBox.Show("Done");
}
private void log(TestObject obj)
{
logger.Debug(obj.getCount());
}
}
public class TestObject
{
string count;
public TestObject(string i)
{
count = i;
}
public string getCount()
{
return count;
}
}
}