JoeBuntu
October 25th, 2009, 06:17 PM
I have a button on a form that runs the snippet below.
If you press the button rapidly it appears as though the code is running on a separate thread from the form shown by the debug output:
Start: 0
Finish: 4
Start: 4
Finish: 4
Start: 4
Start: 0
Finish: 4
Start: 4
Finish: 4
Start: 4
Start: 0
Start: 0
Finish: 4
Start: 4
Finish: 4
Finish: 8
Finish: 12
Finish: 16
what the code does is clear a list view, query a database and fill the list view with the results.
with the paramaters I am using I should end up with 4 items in my listview every time, but when I do it rapidly I am ending up with a multiple of 4!!
I thought that any code that handles form events runs on the same thread as the form and therefore the button shouldn't respond to any further click events until the code is completed?
Just to see if it would work I added the lock(lock_obj) block and it doesn't seem to work either.
I could probably get around the whole problem by disabling the button and then enabling it, but I would like to know why/how this is happening and why isn't lock(lock_obj) working?
thanks
private object lock_obj = new object();
private void btnSearch_Click(object sender, EventArgs e)
{
lock (lock_obj)
{
System.Diagnostics.Debug.Print("Start: " + m_PoIdList.Count.ToString());
lviewResults.Items.Clear();
m_PoIdList.Clear();
lblResults.Text = "Results: ";
Application.DoEvents();
if (m_QueryBuilder.AnyParametersSet)
{
foreach (InboundPOReadonly po in InbPosDB.GetPoItemsReadOnly(m_QueryBuilder.GenerateCommand()).Values)
{
string[] props = new string[7]
{po.Id.ToString(), po.PO, po.VendorName, po.ReceivedDate.ToShortDateString(),
po.User, po.Timestamp.ToShortDateString() + " " + po.Timestamp.ToLongTimeString(), po.ItemCount.ToString()};
m_PoIdList.Add(po.Id);
lviewResults.Items.Add(new ListViewItem(props));
}
System.Diagnostics.Debug.Print("Finish: " + m_PoIdList.Count.ToString());
lblResults.Text = "Results: " + m_PoIdList.Count;
}
else
MessageBox.Show("No Parameters have been set");
}
}
If you press the button rapidly it appears as though the code is running on a separate thread from the form shown by the debug output:
Start: 0
Finish: 4
Start: 4
Finish: 4
Start: 4
Start: 0
Finish: 4
Start: 4
Finish: 4
Start: 4
Start: 0
Start: 0
Finish: 4
Start: 4
Finish: 4
Finish: 8
Finish: 12
Finish: 16
what the code does is clear a list view, query a database and fill the list view with the results.
with the paramaters I am using I should end up with 4 items in my listview every time, but when I do it rapidly I am ending up with a multiple of 4!!
I thought that any code that handles form events runs on the same thread as the form and therefore the button shouldn't respond to any further click events until the code is completed?
Just to see if it would work I added the lock(lock_obj) block and it doesn't seem to work either.
I could probably get around the whole problem by disabling the button and then enabling it, but I would like to know why/how this is happening and why isn't lock(lock_obj) working?
thanks
private object lock_obj = new object();
private void btnSearch_Click(object sender, EventArgs e)
{
lock (lock_obj)
{
System.Diagnostics.Debug.Print("Start: " + m_PoIdList.Count.ToString());
lviewResults.Items.Clear();
m_PoIdList.Clear();
lblResults.Text = "Results: ";
Application.DoEvents();
if (m_QueryBuilder.AnyParametersSet)
{
foreach (InboundPOReadonly po in InbPosDB.GetPoItemsReadOnly(m_QueryBuilder.GenerateCommand()).Values)
{
string[] props = new string[7]
{po.Id.ToString(), po.PO, po.VendorName, po.ReceivedDate.ToShortDateString(),
po.User, po.Timestamp.ToShortDateString() + " " + po.Timestamp.ToLongTimeString(), po.ItemCount.ToString()};
m_PoIdList.Add(po.Id);
lviewResults.Items.Add(new ListViewItem(props));
}
System.Diagnostics.Debug.Print("Finish: " + m_PoIdList.Count.ToString());
lblResults.Text = "Results: " + m_PoIdList.Count;
}
else
MessageBox.Show("No Parameters have been set");
}
}