tags:

views:

473

answers:

4

See Also: http://stackoverflow.com/questions/725735/how-to-enable-a-timer-from-a-different-thread-class

The timer is assigned to a form and I'd like to Enable it at a specific location,but from another class.I don't want to make it public

This is the code I use to access memo

    public string TextValue
    {
        set
        {
            if (this.Memo.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.Memo.Text += value + "\n";
                });
            }
            else
            {
                this.Memo.Text += value + "\n";
            }
        }
    }

    public static void addtxt(string txt)
    {
        var form = Form.ActiveForm as Form1;
        if(form != null)
            form.TextValue = txt;
    }
+1  A: 

Make the timer "internal" then it is accessible to other classes in the assembly.

ck
Yes,it could be accessible to other classes,but if the other classes change the Enable property to true the timer still doesn't Tick().
John
+3  A: 

If you don't want to expose the timer itself, expose a public method or property that you can call to enable the timer. Obviously the Form that enables the Timer will need a reference to the Form that owns the Timer.

HTH, Kent

Kent Boogaart
This still means that the other class needs to have a reference to the form that contains the timer.
Frederik Gheysels
And? That is not the problem. The problem is a desire to enable the timer without exposing the timer itself.
Kent Boogaart
+2  A: 

How would you like to enable the timer ? What action is undertaken in order to enable it ?

Is it possible to add an event to the class from which you want to enable the timer, and, on the form which contains the timer, subscribe to that event ? In the event-handler for that event, you can then enable the timer.

When the other class raises the event, the eventhandler will enable the timer.

public class SomeOtherClassThatDoesStuff
{
   public event EventHandler SomethingHappened;

   public void DoStuff()
   {
      ...
      if( SomethingHappened != null )
         SomethingHappened;
      ...
   }
}

public class Form1
{

    private void Button1_Click(object sender, EventArgs e )
    {

      SomeOtherClassThatDoesStuff o = new SomeOtherClassThatDoesStuff();
      o.SomethingHappened += new EventHandler(EnableTimer);

      o.DoStuff();
    }

    private void EnableTimer(object sender, EventArgs e )
    {
       myTimer.Enabled = true;
    }
}

Something like this. (I haven't tested, nor did I even compile it, but I think you'll catch the drift :) ).

Frederik Gheysels
"SomethingHappened;" is wrong.I'm sorry I never wrote events by hand.The line must have two parameters(object sender and EventArgs e).What parameters should i pass?
John
Problem 2,DoStuff() is not only public,but static in my code.I can't access SomethingHappened if the method is static.
John
As told, i didn't compile :) You can simply pass this and EventArgs.Empty when raising the event.Perhaps you can make the event static as well (dunno if that's possible), but I should check whether it is necessary to have 'DoStuff' static.
Frederik Gheysels
A: 

You could add an accessor method to Enable the timer, this would allow yu to keep the timer private but make the method public, would that work?

Also, you could look at making the accessor protected or internal, depending on who is calling it it may not need to be public.

Steve Haigh