Drop down portion of combo box disappears if pause is long enough

  • Thread starter Mihajlo Cvetanović
  • Start date
M

Mihajlo Cvetanović

Consider this sample code:

public class MainForm : Form
{
private System.Windows.Forms.ComboBox myComboBox;

private void myComboBox_DropDown(object sender, EventArgs e)
{
Thread.Sleep(5000);
myComboBox.Items.Clear();
myComboBox.Items.Add("abc");
}
}

I have some processing instead of Sleep, but Sleep produces the same
effect: drop down portion of combo box disappears the first time I try
to display it. Every other time the list is properly displayed. If the
pause is 4000 ms then the list is displayed even the first time. Anyone
has an explanation about this? How can I prevent the list from
disappearing even the first time?
 
P

Peter Duniho

Mihajlo said:
[...]
I have some processing instead of Sleep, but Sleep produces the same
effect: drop down portion of combo box disappears the first time I try
to display it. Every other time the list is properly displayed. If the
pause is 4000 ms then the list is displayed even the first time. Anyone
has an explanation about this? How can I prevent the list from
disappearing even the first time?

You should not be performing any significant processing in control event
handlers. Those really need to return as soon as possible. I haven't
tried to create a complete code example from your minimal code provided,
but I suspect that you're simply seeing the results of the windowing
system giving up on the message pump (which isn't pumping messages
because you've got it blocked), and drawing a placeholder instead.

Fix your code so that you use some kind of background processing for the
expensive work. You can either pre-calculate whatever is needed for the
DropDown event, or you can handle that event differently, by doing
something inexpensive immediately and starting your expensive
processing, to be used to update the control later.

Pete
 
M

Mihajlo Cvetanović

You should not be performing any significant processing in control event
handlers. Those really need to return as soon as possible.

Thanks. It does makes sense to be responsive :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top