Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have developed an application and i want to add a Datetime (digital clock) indicator on the Mdi form. But i want this indicator to be active whenever my application has delays due to long query results. I used a timer and i updated my datetime indicator label in the timer1_tick event.
So what i'm basically looking for is, a form which shows the time (hh:mm:ss). And the time should not freeze if i click a button which loops 10,000,000 times. I know i can do that by making the loop in another thread but i don't want it that way. I would rather have the loop begin using a button_click event with the main thread and have another thread which updates my date time indicator label every second.
So, to give u a short and precise meaning of what i want.


1. A form which has a button to start a long time consuming for loop like say 10,000,000 and label which shows the loop has ended.

2. A timer to start with the form_load event in another thread and show me the time before i even click the loop button.
3. This timer should have an interval of 1000 (1 second) and should update the datetime indicator label which shows the value of the DateTime.Now value.
4. When i start the loop button, i want to the timer to continue ticking thus updating the datetime indicator label.

so you can say what i'm looking for is an application which has 2 threads who start together, where the second thread has a timer which updates a label every second no matter what happens on the first thread.

I would greatly appreciate it if you can attach the solution or source code so that i can download it and use it. I use C# in Visual studio 2008.
Thank you very much.
Posted
Updated 13-May-11 4:00am
v2

So, that's what you want.
I want $100,000,000 in small, used notes.

Do you think it is likely either of us is going to get what we want without at least some effort?

In my case, I would have to rob a very big bank, win a lottery, or just invent a FarceBook killer app.

In your case, you might have to learn how to code properly.

If you want someone to do your work for you, check out VWorkers - used to be Rentacoder.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-May-11 15:09pm    
Don't worry, I'm not telling anyone about your abilities we discussed before. My 5.
--SA
Your form freezes because it's not multi-threaded. In a single threaded model, the lone thread would be busy dealing with your actions causing the form to freeze. In your case it cannot return back from the method that runs your time consuming loop. You need delegate tasks like this to another thread, other than the ones related to the UI and if you do this the UI thread will be free to update the UI.

Check out these links:

http://msdn.microsoft.com/en-us/library/ms951089.aspx[^]

In case you are looking for a good search term try this:

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=c%23+windows+forms+delegate+work+to+another+and+free+the+ui+thread[^]
 
Share this answer
 
Comments
Olivier Levrey 13-May-11 10:21am    
Good link to quickly understand asynchronous tasks using threads and updating the UI.
My 5.
Olivier Levrey 13-May-11 10:34am    
OP posted an answer instead of a comment. Please see.
Karthik. A 13-May-11 10:59am    
@Oliver Leverey - thanks!
Karthik. A 13-May-11 11:02am    
Yes this is technique that would help you achieve the same. When your look run 10,000,000 times, it cannot just try to access your UI elements. It has to be taken care of by the thread that handles this task. You are saying that you are doing this in a thread, but this thread 'cannot' / 'should not' access UI elements. It should just pass a message to the UI thread to do this. Sorry if i am confusing you.
Karthik. A 13-May-11 11:03am    
Check out the "Simplified Multithreading" section in the link above. The thread that wishes to update the UI must check the "InvokeRequired" property and if its false, it will invoke a delegate to do the task.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900