Timer not working in thread

S

sony.m.2007

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
 
J

Jon Skeet [C# MVP]

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?

In the above code (which clearly isn't your *real* code, btw, due to
the casing - it's always worth posting genuine code) you've started the
thread and then *immediately* stopped the timer and made the label
invisible. Why? Surely UpdateMethod should do that when it finishes.
 
M

Marc Gravell

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
 
S

sony.m.2007

In the above code (which clearly isn't your *real* code, btw, due to
the casing - it's always worth posting genuine code) you've started the
thread and then *immediately* stopped the timer and made the label
invisible. Why? Surely UpdateMethod should do that when it finishes.

Hi,
The updatemethod is a method to process a file , then update the data
into sql server, it's big method.So I didn't mention the code for
updatemethod .My problem is the label not displayed on the form.I also
tried thread.Join() like below
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()
t.Join();
timer1.enabled=false;
label1.visible=false
}
In this case also label not blinking

Cheers,
Sony
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 
S

sony.m.2007

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

Hi,
when I tried
this.Invoke((MethodInvoker) delegate {timer1.Enabled =
false;label1.visible =false});
I'm getting copiler errors
1. A namespace does not directly contain members such as fields or
methods
2.Invalid expression term ')'
3.Invalid expression term 'delegate'
4.Invalid token '=' in class, struct, or interface member declaration
5.Type expected
6.Type or namespace definition, or end-of-file expected

Cheers,
Sony
 
M

Marc Gravell

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;
}
 
J

Jon Skeet [C# MVP]

Marc Gravell said:
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

Even nicer, it could automatically detect if the handle hasn't been
created yet, or the form has been disposed, and either indicate somehow
that it hadn't executed the delegate, or maybe throw an exception.

Of course, that will always involve a race condition:

Has form been disposed? No.
Form is closed
Ah, Control.Invoke...
BANG!

I haven't really worked out how to get round that properly in WinForms.
 
J

Jon Skeet [C# MVP]

when I tried
this.Invoke((MethodInvoker) delegate {timer1.Enabled =
false;label1.visible =false});
I'm getting copiler errors
1. A namespace does not directly contain members such as fields or
methods
2.Invalid expression term ')'
3.Invalid expression term 'delegate'
4.Invalid token '=' in class, struct, or interface member declaration
5.Type expected
6.Type or namespace definition, or end-of-file expected

Well for a start you'd need an extra semi-colon after
label1.visible=false (as well as making it use real properties -
*please* start posting the actual code).

Then there's the question of whether you're using C# 3 to start with...
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

Marc Gravell said:
Actually he cited the C# 2 version - so my guess is C# 1.2

Rats - thought I'd managed to shut my newsreader down in time to stop
that message making it out.

I thought he was talking about your extension method version. I didn't
take the time to look back at the whole thread, but when I saw your
response I realised the error of my ways.

(see, I did read the book!)

:)
 
S

sony.m.2007

Rats - thought I'd managed to shut my newsreader down in time to stop
that message making it out.

I thought he was talking about your extension method version. I didn't
take the time to look back at the whole thread, but when I saw your
response I realised the error of my ways.



:)

Hi,
I'm using .NET framework 1.1 and Visual studio 2003

Cheers,
Sony
 
I

Ignacio Machin ( .NET/ C# MVP )

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)
 
S

sony.m.2007

Are you showing/hidding the label multiple times????


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top