Link to home
Start Free TrialLog in
Avatar of Dynotoe
Dynotoe

asked on

C# -- Question on "should I" and how to place a delay in TextBox1.TextChanged method

Hi everyone,

I have upto 25 textboxes on a form1 which each use the TextChanged event that are called using the evert args e or whatever.

Questions

1).  If I placed a 1 or 2 second delay in this textbox1.textchanged evet/method/function, does the whole project have to wait for it to finish or is this system call on a separate process/thread etc.  What if a few were being called on the same form for different textboxes at roughly the same time.  Would the delays get in each others way?

2).  How would I set up such a delay (Without using a timer cause I'm looking for an alternate to 20+ timers, one for each textbox sort of thing)  What would be an example of some "delay code in a function/Method ?

Please allow my thanks in advance for your gracious help.

Cheer,

Dynotoe
Avatar of Expert1701
Expert1701

Dynotoe, I will answer your questions in reverse:

2) A simple delay can be created by calling the System.Threading.Thread.Sleep method.  For example, to create a 2 second delay you would use the following code:

  System.Threading.Thread.Sleep(2000);

1) The TextBox.TextChanged event will be processed by the UI thread.  This means that while the method is run, the UI will be suspended.  If the method contained a 2 second delay, all UI activity (e.g. typing in another TextBox, or the execution of another TextBox's TextChanged event) will be delayed.  To avoid this, you could use the following pattern:

  private void textBox1_TextChanged(object sender, System.EventArgs e)
  {
    System.Threading.Thread processThread = new System.Threading.Thread(new System.Threading.ThreadStart(Process_textBox1_TextChanged));
    processThread.Start();
  }

  private void Process_textBox1_TextChanged()
  {
    System.Threading.Thread.Sleep(2000);

    //..
  }
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dynotoe

ASKER

Hello!

Well this is what I am trying to accomplish....

I have a gui form that displays changing stock quotes in text boxes (One per symbol).  But too many!

I would like to ...

1)  When a quote is changed the backgroud color will change to LightGolden Yellow but only for two seconds where it should then be changed back to original color (white).  This lets your eye catch what has just changed.

2)  When the new quote comes in if it was a down change (based on what was originally in the text box the Forcolor would change to red etc.  If up forcolor is changed to green etc.  These can stay like this till a new quote is captured.


The problem is that I don't want 20-30 timers setup for each textbox and setting up a user control would be difficult becasue all the forms and textboxes are already set up.  So Im looking for an alternative.  I do have the Text changed event set up for each textbox and already have the functionality of the red Green and Yellow background color functioning.  What I don't have is the ability to have the backcolor change back to White after time has passed since a different quote changed as compared to what was previously in there.  I have those sitting in Vars also ready to go.

Make sence?

Thanks for your help.

Best,
Dynotoe
Ok...the code I posted can handle that just fine...just change the colors to the ones you want...

When you get a new quote value, check the previous value in the TextBox and change the ForeColor accordingly.  Set the BackColor to LightGolden Yellow and set the Tag() property as I have shown in the code:

    tb.Tag = DateTime.Now.AddSeconds(2);

Then in the Timer code, change the Color to white and reset the Tag:

    tb.BackColor = Color.White;
    tb.Tag = null;
Avatar of Dynotoe

ASKER

Hi Idle,

Great stuff.  Quick question Regarding the update od last price (which is then referenced as prior price when the new one comes in ) how can I check which textbox has been smet into the text changed as sender?  The reason is that I have a variable for each textbox that will store the prior price in it.  Or is there a better way to reference and compare last price with current price to determin an uptick or a down tick.  etc.

Dynotoe
Look at my code for the TextChanged() event:

        void textBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.BackColor = Color.Green;
            tb.Tag = DateTime.Now.AddSeconds(2);
        }

I cast the sender to a TextBox referenced by the "tb" variable.  If you need the name then use "tb.Name".

There are many ways to handle the previous value.  One possibility would be to use a HashTable.  Then you can lookup the previous value using the stock name as the key.

Or you could create a simple class to hold both a reference to the old value and the corresponding TextBox and place an instance of that into the HashTable.  Then you will be able to jump directly to the TextBox based on the stock name.

So many possibilities...how are you correllating the stock name to your variables right now?
Avatar of Dynotoe

ASKER


Hi Idle,

Unfortunately HARD CODED lol

private float EURUSD_oldValue = 0.0F;
private float USDJPY_oldValue = 0.0F;
private float GBPUSD_oldValue = 0.0F;

And so on...

The problem is that if a new symbol is added manually or dynamically then I would also have to make a var for it.  ;(

Best,

Dynotoe
Avatar of Dynotoe

ASKER

Then I call this method from the textChanged event...

            // Update current prices which will then be used to determin UP/Down tick for next quote
            public void updatelastprices(ref TextBox textBoxPriorPrice )
            {
                  switch (textBoxPriorPrice.Name)
                  {
                        case "EURUSD":
                              EURUSD_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;
                        case "USDJPY":
                              USDJPY_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;
                        case "GBPUSD":
                              GBPUSD_oldValue = Convert.ToSingle(textBoxPriorPrice.Text);
                              break;

etc.
How do you "lookup" the stock prices?  Are you passing the stock name to a function?

Do you have 25 hard coded lines to lookup each quote?

We can make this process more dynamic and flexible but I need a "bigger picture"...   =)
Avatar of Dynotoe

ASKER

I'll send you the whole picture lol :)

Dynotoe

istomarFarza  "ate" Com cast "doot" Net
Dynotoe
AKA Sean
Avatar of Dynotoe

ASKER

oops IshtomarFarza "ate" ...

By the way your solution to help me works terrific!

Cheers,

Sean