![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2006
Posts: 7
Rep Power: 0
![]() |
Thread.sleep issue
Hi,
Got a little issue I am scratching my head over. In a program I am working on, I have a place where I have some basic output being writen to a window and what to have each line show up every second or so. But I can't see to get the timing to work. I put together a little simple app to test some things, so maybe somebody could tell me why this is not working. I start with one windows form, where a person enters a number and hits the button to call the next form. private void btnDisplay_Click(object sender, EventArgs e)
{
int numItems = Convert.ToInt16(txtItems.Text);
OutputList ost = new OutputList(numItems);
ost.Show();
}The window that opens is simply a text box that I am writing lines to: public OutputList(int numItems)
{
InitializeComponent();
int _numItems = numItems;
int i = 0;
string showItems;
while (i < _numItems)
{
showItems = Convert.ToString("This is Item # " + i);
txtListItem.Text += (showItems + "\r\n");
Thread.Sleep(1000);
i++;
}
}But when I run this, the window opens after the number of seconds from the input and all the lines of text are already written. I want the window to open first and then have each line of text written in it. Can I do it this way? Am I missing something simple? Or do I need to find another way of doing this? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
Try changing the parameter of Sleep()
|
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
The form is not shown until the ost.Show line. This does not happen until after the form is constructed. The form is not constructed until the list is fully populated, which takes a long time due to Thread.Sleep. You are probably looking to use a timer or background thread to add the list items after the form is already visible.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,122
Rep Power: 5
![]() |
Your problem is that the UI runs on a single thread. This means event handlers too, such as the events that drive repainting of the controls. You set the control's text, then put the thread to sleep, so no updates happen. When the thread wakes up, it immediately proceeds to the next loop iteration (and thus is busy changing the control's text, and not updating the visual appearance). Once it's done updating the text (not the screen display of this text, mind you), you put it to sleep, and the cycle continues. In short, what is happening is that events are queued up, and handled in order. Until the event handler (btnDisplay_Click(), in this case) returns, no more events will be processed. If you've ever done any programming using the raw Win32 API, it's all driven by the 'message pump'; .NET thunks down to this within its guts.
A similar thing can be seen if you have a lengthy loop inside an event handler. Other events are deferred until the first one is handled. Two good choices for resolving this would be to expose a property or method in your OutputList class whereby your regular class could update its display appropriately, or to have a timer in the OutputList class, and it could periodically update itself (you update txtListItem inside the timer tick event handler). Which is better really depends on what you're trying to do, and on whether OutputList will receive all necessary information in its constructor, or whether it needs periodic updating from another source (user, other form, database, whatever). On a totally unrelated note, you might consider the TryParse() static method for converting primitive types; this allows you to avoid exception-handling semantics: int value;
if(!int.TryParse(txtHopefullyItsANumber.Text, out value))
MessageBox.Show("You must enter a valid number.", "Error");[edit] What Dameon said, too. ;) [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2006
Posts: 7
Rep Power: 0
![]() |
Thanks for the info, this explains what I was seeing.
I am still working on the actual program I am doing and was not sure if the thread.sleep method was going to be the best way of doing what I wanted anyway. But it was still some good info. Just as long as I can remember this next time I run into the same situation, it was a worth while question. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Major IE6 issue + CSS | Kezzer | HTML / XHTML / CSS | 1 | May 29th, 2007 8:52 AM |
| Infinate Loop Issue | emdiesse | Java | 1 | May 22nd, 2007 3:54 AM |
| .NET Timer Form closing issue | MBirchmeier | C# | 4 | Nov 21st, 2005 11:00 AM |
| DOM (createElement issue...) | tempest | JavaScript and Client-Side Browser Scripting | 2 | Nov 1st, 2005 7:50 PM |
| JNDI and LDAP issue | Mortavitch | Java | 5 | Oct 20th, 2005 12:10 PM |