468,119 Members | 1,954 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Thread.Sleep

If you want to keep reading the value of a property (10 seconds) that
keeps changing,
how would you do it. I tried this, but it did not work
Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

Dec 28 '05 #1
12 5858
JPSutor,

I wouldn't use a polling mechanism like this. Rather, I would have some
sort of notification on class which indicates when the property changes.
The framework has a standard for things like this.

The first (pre .NET 2.0) is to have an event on your class named
<property name>Changed. This event is of type EventHandler, and you fire it
when the event changes.

If you are using .NET 2.0, you should use the INotifyPropertyChanged
interface to indicate when a property changed (it's more efficient in
general than the old method).

If you want to poll the property, then I would use a timer, instead of
causing a thread to sleep, and check the property value there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JPSutor" <jp*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
If you want to keep reading the value of a property (10 seconds) that
keeps changing,
how would you do it. I tried this, but it did not work
Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

Dec 28 '05 #2
MJB

I might also add that a while loop with a sleep duration works quite
well. I noticed in pre .Net 2.0 the timer just wasn't reliable enough
in certain situations. A while loop seemed to work well as an alternate
solution.
Nicholas Paldino [.NET/C# MVP] wrote:
JPSutor,

I wouldn't use a polling mechanism like this. Rather, I would have some
sort of notification on class which indicates when the property changes.
The framework has a standard for things like this.

The first (pre .NET 2.0) is to have an event on your class named
<property name>Changed. This event is of type EventHandler, and you fire it
when the event changes.

If you are using .NET 2.0, you should use the INotifyPropertyChanged
interface to indicate when a property changed (it's more efficient in
general than the old method).

If you want to poll the property, then I would use a timer, instead of
causing a thread to sleep, and check the property value there.

Hope this helps.

Dec 28 '05 #3
Hi,

Thread th = new Thread(new ThreadStart(ReadMe));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

This does not work cause Thread.Sleep refer to the current thread , the
thread where the method is called, for this to work Thread.Sleep should be
in the ReadMe() method.
No need to call Abort btw.
ITOH, I would not do it this way, I would use a timer .

Or even better an event.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 28 '05 #4
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #5
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #6
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #7
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #8
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #9
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #10
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #11
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #12
I suppose that the error message you've got is about thread synchronization.
If so, try this :

public class YourForm : System.Windows.Forms.Form
{
private delegate void ProgressBarDelegate(Int32 floodPercent);

public void UpdateProgressBar(Int32 floodPercent)
{
// Need thread synchronization ?
//
if (this.InvokeRequired)
{
this.Invoke(
new ProgressBarDelegate(this.UpdateProgressBar),
floodPercent);

return;
}

// Update ProgressBar flood percent here...
}
}

public class YourWorkingThread
{
private YourForm parentForm;
private DataTable dataToProceed;

YourWorkingThread(YourForm parentForm, DataTable dataToProceed)
{
this.parentForm = parentForm;
this.dataToProceed = dataToProceed;
}

public void ThreadedJob()
{
Int32 rowCount = dataToProceed.Rows.Count;

for (Int32 i = 0; i < rowCount; i++)
{
// Proceed dataToProceed.Rows[i] ...

// Update progress bar
//
if (i % 100 == 0)
parentForm.UpdateProgressBar((Int32)((Double)i / rowCount + 0.5));
}
}
}

"JPSutor" wrote:
Nicholas,
What I am trying to accomplish is this.
I have a form with a progress bar on it.
When I make a call to a method in another class to iterate through a
recordset, I want the progress bar to show the progress.
When I try referencing the form from my class I get an error message.
What I'm trying to do here is to set a property value and read it in
each time the timer fires. I realize this isn't the most effecient
method, but my first try did not work

Dec 28 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Anthony Baxter | last post: by
26 posts views Thread by news.microsoft.com | last post: by
8 posts views Thread by Cider123 | last post: by
4 posts views Thread by Matthew Groch | last post: by
1 post views Thread by Matthijs | last post: by
9 posts views Thread by Chris Dunaway | last post: by
6 posts views Thread by k.mellor | last post: by
9 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
2 posts views Thread by Steve | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.