Thread.Sleep

  • Thread starter Thread starter JPSutor
  • Start date Start date
J

JPSutor

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);
 
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.
 
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.
 
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.
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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 ...

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

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

Back
Top