473,387 Members | 1,745 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,387 software developers and data experts.

Timer not working in thread

Hi,
My application processes a file and updates the data into sql server.
The process takes around 30-90 seconds.I decided to put a timer to
display the user a label(making label visible false and true) with
message like "processing..." and do the other process using thread.
The timer won't works when the thread is started, so the label is not
displayed in the form
I have a label label1,a timer timer1 and command button in a form
timer1.interval=100;
private void timer1_Tick(object sender, System.EventArgs e)
{
label1.Visible = ! label1.Visible;
}

private void commandButton_Click(object sender, System.EventArgs e)
{
timer1.enabled=true;
label1.visible=true;
this.refresh
Thread t=new thread(new threadstart(updateMethod))
t.start()
timer1.enabled=false;
label1.visible=false
}

When I execute the above code label is not displayed in the
form.After the completion of thread only label get displayed.
Is there anything wrong in the above method of blinking a label?

Cheers,
Sony
Apr 4 '08 #1
8 3032
You are disabling it too soon... when you disable the timer, I doubt
that updateMethod has even begun... you need to disable this at the end
of updateMethod instead, first jumping back onto the UI thread (due to
thread affinity):

void updateMethod() {
... your code
this.Invoke((MethodInvoker)delegate {
timer1.enabled=false;
label1.visible=false;
});
}

Marc
Apr 4 '08 #2
Ooh - just thought of a very useful extension method to Form:

public static void Invoke(this Form form, Action action) {
if(form == null) throw new ArgumentNullException("form");
if(action == null) throw new ArgumentNullException("action");

form.Invoke((Delegate)action);
}

Then you can use:

this.Invoke(delegate {...});
or
this.Invoke(() ={...});

Not a huge thing, but I like it ;-p

Marc
Apr 4 '08 #3
With Join you are simply hanging the UI; see my other post. And I don't
think Jon was suggesting posting that code - just something that
compiled and demonstrated the problem! You'd be amazed at how often we
get questions where the example code (because the OP hasn't tried it)
either doesn't show the same problem, or has even bigger issues [or just
doesn't compile].

Marc
Apr 4 '08 #4
(actually the target could have been Control or ISynchronizeInvoke)
Apr 4 '08 #5
I'm getting copiler errors

Which version of .NET are you using? 2.0 and above should be fine (if
you add a semicolon and fix the case that was copied verbatim from your
post). If you are using 1.1 (VS2003), then you need to do something
slightly different [I can't check if it compiles as I don't have 1.1 nearby]

Marc

void UpdateMethod()
{
//...
this.Invoke(new MethodInvoker(DisableTimer));
}
void DisableTimer()
{
timer1.Enabled = false;
label1.Visible = false;
}
Apr 4 '08 #6
Then there's the question of whether you're using C# 3 to start with...
Actually he cited the C# 2 version - so my guess is C# 1.2
(see, I did read the book!)

Marc

Apr 4 '08 #7
I have a label label1,a timer *timer1 and command button in a form
timer1.interval=100;
private void timer1_Tick(object sender, System.EventArgs e)
* * * * * * * * {
* * * * * * * * * * * * label1.Visible = ! label1.Visible;
* * * * * * * * }
Are you showing/hidding the label multiple times????
private void commandButton_Click(object sender, System.EventArgs e)
{
timer1.enabled=true;
label1.visible=true;
this.refresh
Thread t=new thread(new threadstart(updateMethod))
t.start()
timer1.enabled=false;
label1.visible=false
The last line of the above code is the line of the problem, you are
disabling the timer right there, most probably before it might even
tick for the first time.
You have to disable as the end of the worker method ( UpdateMethod)
Apr 4 '08 #8
On Apr 4, 3:45 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
I have a label label1,a timer timer1 and command button in a form
timer1.interval=100;
private void timer1_Tick(object sender, System.EventArgs e)
{
label1.Visible = ! label1.Visible;
}

Are you showing/hidding the label multiple times????
private void commandButton_Click(object sender, System.EventArgs e)
{
timer1.enabled=true;
label1.visible=true;
this.refresh
Thread t=new thread(new threadstart(updateMethod))
t.start()
timer1.enabled=false;
label1.visible=false

The last line of the above code is the line of the problem, you are
disabling the timer right there, most probably before it might even
tick for the first time.
You have to disable as the end of the worker method ( UpdateMethod)
Yes showing/hidding the label multiple times
Cheers,
Sony
Apr 4 '08 #9

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

Similar topics

3
by: RitaG | last post by:
Hi. I Have a VB.Net Windows Application that starts up using Sub Main. It's a program that runs in the background and checks something every minute. I use a Timer to accomplish this and it's...
3
by: Benny | last post by:
Hello Experts, Currently I am working on a windows application using VS.NET 2002 with C#. On one of my form, I have a refresh button which will dispose created controls and re-create them. I...
2
by: Amadej | last post by:
Hello everyone I have a beginner's questions about the System.Threading.Timer class behavior. I have been observing the thread count with Windows Task Manger and noticed that timers, after being...
8
by: theinvisibleGhost | last post by:
I think I've found a bug in the timer control. I've got a class which uses a timer control. It imports it from System.Windows.Forms This timer ticks once a second, and then updates a label...
4
by: Robert W. | last post by:
I'm building a WinForms app that is a companion to a Pocket PC app. The WinForms app initiates a separate thread that calls a Notification window. This window resides in the lower right corner of...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
6
by: Aart Nicolai | last post by:
Hi all, I have developed a vb.net console application which will run some code every seconds. To get this working I used a timer "System.Timers". When I start my console application it exits...
4
by: archana | last post by:
Hi all I am having application which is using server based timer that is timer from namespace system.timer. I am having windows service which i want to run after every 1 hour and after...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
3
by: Steve | last post by:
Hi All I am using VB.net 2008 and use timer controls within my applications Question Does the code in a Timer control.tick event run on a different thread to the main Application thread (UI...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.