I made a C# Windows Form. It uses a web method, and then I want to write the webmethod results to a listbox. The web method runs in a loop until I push a stop button.
The issue I'm having is, I'm unable to click any of the controls on the form, and even moving around the form itself is impossible.
The function of adding the webmethods results to the list box are working though. But it's pretty much useless since I can't scroll down the list of added items.
I put a 1 second sleeper time between each web method call, but that's not helping...
bool stop = false;
private void button1_Click(object sender, EventArgs e)
{
stop = false;
while (stop == false) {
GetTime(textBox1.Text);
System.Threading.Thread.Sleep(1000);
}
}
private void GetTime(string mid)
{
string i;
com.Service test = new com.Service();
i = test.GetZonedDateTimeByMID(mid).ToString();
lstLog.Items.Add(i);
}
private void btnStop_Click(object sender, EventArgs e)
{
stop = true;
}
Calling the static method Thread.Sleep(X)
will cause the current thread (in your case, your application's UI thread) to sleep. So yes, that will make your application unresponsive. You need to do your work from a background worker thread, either by using a Task
or calling ThreadPool.QueueUserWorkItem
.
However, be aware that you can't update UI elements directly from any thread other than your application's UI thread, you will need to call the control's Invoke
method.