473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with this?

I was reading the "thread problem" post and maybe it has something to do
with this, but I didn't quite understand it all. Here is the code I wrote:

private void btnStart_Click(object sender, EventArgs e)
{
int time = Int32.Parse(txtTime.Text);
Thread.Sleep(60000);

for (int i = time - 1; i > 0; i--)
{
txtMessage.Text = i + " minutes remaining.";
Thread.Sleep(60000);
}

txtMessage.Text = time + " minutes have elapsed.";

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}

Originally this was a console application, and I just changed the
txtMessage.Text parts from the original Console.WriteLine parts.

What happens is that it will run through to the end (although sometimes
I get "Not Responding") and it will display the final message and the
beeps, but it does not run through the first for loop, which shows
minutes remaining. The debugger also seems to get hung up when I use it,
and I can't find a way to step over the Thread.Sleep method, so it sort
of kills the application, even in debug mode.
Nov 17 '05 #1
4 1102
John Salerno <jo******@NOSPAMgmail.com> wrote:
I was reading the "thread problem" post and maybe it has something to do
with this, but I didn't quite understand it all. Here is the code I wrote:

private void btnStart_Click(object sender, EventArgs e)
{
int time = Int32.Parse(txtTime.Text);
Thread.Sleep(60000);

for (int i = time - 1; i > 0; i--)
{
txtMessage.Text = i + " minutes remaining.";
Thread.Sleep(60000);
}

txtMessage.Text = time + " minutes have elapsed.";

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}

Originally this was a console application, and I just changed the
txtMessage.Text parts from the original Console.WriteLine parts.

What happens is that it will run through to the end (although sometimes
I get "Not Responding") and it will display the final message and the
beeps, but it does not run through the first for loop, which shows
minutes remaining. The debugger also seems to get hung up when I use it,
and I can't find a way to step over the Thread.Sleep method, so it sort
of kills the application, even in debug mode.


Yes, it's running through the first for loop - but because you're
running on the UI thread, it's not actually getting a chance to update
the UI.

One of the golden rules of UI threading: don't hog the UI thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Jon Skeet [C# MVP] wrote:
John Salerno <jo******@NOSPAMgmail.com> wrote:
I was reading the "thread problem" post and maybe it has something to do
with this, but I didn't quite understand it all. Here is the code I wrote:

private void btnStart_Click(object sender, EventArgs e)
{
int time = Int32.Parse(txtTime.Text);
Thread.Sleep(60000);

for (int i = time - 1; i > 0; i--)
{
txtMessage.Text = i + " minutes remaining.";
Thread.Sleep(60000);
}

txtMessage.Text = time + " minutes have elapsed.";

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}

Originally this was a console application, and I just changed the
txtMessage.Text parts from the original Console.WriteLine parts.

What happens is that it will run through to the end (although sometimes
I get "Not Responding") and it will display the final message and the
beeps, but it does not run through the first for loop, which shows
minutes remaining. The debugger also seems to get hung up when I use it,
and I can't find a way to step over the Thread.Sleep method, so it sort
of kills the application, even in debug mode.

Yes, it's running through the first for loop - but because you're
running on the UI thread, it's not actually getting a chance to update
the UI.

One of the golden rules of UI threading: don't hog the UI thread.


Hmm, I figured the threading thing might be a problem when I converted
it to a WinForm app. Is there another way to create this delay for my timer?
Nov 17 '05 #3
John Salerno <jo******@NOSPAMgmail.com> wrote:
One of the golden rules of UI threading: don't hog the UI thread.


Hmm, I figured the threading thing might be a problem when I converted
it to a WinForm app. Is there another way to create this delay for my timer?


See http://www.pobox.com/~skeet/csharp/threads/timers.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Jon Skeet [C# MVP] wrote:
John Salerno <jo******@NOSPAMgmail.com> wrote:
One of the golden rules of UI threading: don't hog the UI thread.


Hmm, I figured the threading thing might be a problem when I converted
it to a WinForm app. Is there another way to create this delay for my timer?

See http://www.pobox.com/~skeet/csharp/threads/timers.shtml


Thanks, I was wondering what the difference between these was.
Unfortunately, it looks like the one I need (for Forms) is much more
complicated than the System.Threading way.
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruce W...1 | last post by:
In my effort to learn PHP I'm playing with some simple email scripts. They worked a few days ago but they stopped working. The only thing I've done to this Windows 2000 PC in this time was a...
2
by: Li Ma | last post by:
Just want to share my experience on XML-RPC. We were working on a messaging middleware. We were using XML-RPC as communication protocol amoing client apps and server. Client could be written by...
9
by: Eric | last post by:
Problem: -------- I'm trying to create an executable jar file. I can do so as long as I don't use the "package" statement in my source code. Once I put the package statement in I can't execute...
4
by: Jaydeep | last post by:
Hello, I am facing a strange problem. Problem accessing remote database from ASP using COM+ server application having VB components (ActiveX DLL) installed. Tier 1 : ASP front End (IIS 5.0) Tire...
0
by: Anil0920 | last post by:
I have a very serious problem which really needs to be solved. We had a huge number of projects in 2002 that we recently converted to VS.NET 2003. The conversion was smooth and all is working well....
4
by: varungupta | last post by:
Hi Group members ! We are hosting a Apache/PHP/MySql based website at our college since January,2005. It was working fine since then. The web server is plaed in our college itself and we are...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
4
by: B.D. | last post by:
Can anyone explain way the transformation to upper case doesn't work correctly in the following code if PROBLEM is defined but works correctly if it's not defined? I'm using VC 7.1 #include...
8
by: Rinaldo | last post by:
Hi, When I start my program in the debugger, there is no problem, but when not I get an exception. It appears in: private void Upload(string filename, string FTnaam) { MessageBox.Show("in...
6
by: pkchandra999 | last post by:
My Server OS : Centos 64 bit Apache version is Apache v2.2.9 Php Version : 5.2.5 I am providing feeds to my forum through RSS. The feeds are in html and those feeds will be parsed to bbcode...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.