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

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 6160
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
26
by: news.microsoft.com | last post by:
Hi, Currently I have a thread thats spinning and doing a Thread.Sleep(someTime). I was thinking of changing this to Thread.Sleep(Timeout.Infinite); then when I have actual data in a...
8
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
1
by: Matthijs | last post by:
Hi, I have a problem coding a UDP Server/client. The server needs to send big amounts of data over UDP. The problem however is that you can't send as fast as you like. (All packets will be dropped...
9
by: Chris Dunaway | last post by:
According to the docs, calling Thread.Sleep(0) causes the thread to be "suspended to allow other waiting threads to execute." What happens if I call Thread.Sleep(500)? Do other threads not get a...
6
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second...
0
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the...
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.