Thread: triggering another control's method

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    triggering another control's method

    How do I trigger another control's method in C#?

    I want to have a background thread running a timer and if the timer function finishes in the thread I'd like it to trigger the stop button which can be used also terminate the thread prematurely.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Something like this, or did I miss something?
    Code:
    protected void Timer1_Timeout(object s, Args e)
    {
      StopBackgroundThread();
    }
    
    protected void Button1_Click(object s, Args e)
    {
      StopBackgroundThread();
    }
    
    private void StopBackgroundThread()
    {
       Button1.Enabled = false;
       Timer1.Stop();
    
       //etc...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Missed completely.
    Something like:
    Code:
    protected void Timer1_Timeout(object s, Args e)
    {
      If (for some reason this timer's Stop is triggered by code)
      trigger Button1_Click event/method
    }
    
    protected void Button1_Click(object s, Args e)
    {
      StopBackgroundThread();
    }
    
    private void StopBackgroundThread()
    {
       Button1.Enabled = false;
       Timer1.Stop();
    
       //etc...
    }
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    If you wish to change the Button1.Enabled property from a background thread you'll need to invoke it first otherwise it will throw an illegal cross thread exception.

    An easy way is to have a seperate method for setting the Button1.Enabled property. Example:

    Code:
    private delegate void SetButtonEnableStatusDelegate(bool enabled);
    
    private void SetButtonEnableStatus(bool enabled)
    {
        if (Button1.InvokeRequired)
            Button1.BeginInvoke(new SetButtonEnableStatusDelegate(SetButtonEnableStatus), enabled);
        else
            Button1.Enabled = enabled;
    }
    You can use the SetButtonEnableStatus method to set the Button1.Enabled property from within the button's owner thread, or a background thread.

    Code:
    SetButtonEnableStatus(true); // button enabled
    
    SetButtonEnableStatus(false); // button disabled

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thanks Theoobe (for answering my next question before I posed it) .
    For the rest of you others out there I was looking for something like:
    Code:
    protected void Timer1_Timeout(object s, Args e)
    {
      If (for some reason this timer's Stop is triggered by code)
      Button1_Click(null, null); //<====trigger Button1_Click event/method
    }
    
    protected void Button1_Click(object s, Args e)
    {
      StopBackgroundThread();
    }
    
    private void StopBackgroundThread()
    {
       Button1.Enabled = false;
       Timer1.Stop();
    
       //etc...
    }
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 12:59 AM
  3. Replies: 2
    Last Post: 01-22-2008, 04:22 PM
  4. Dynamically adding method to created controls
    By earth_angel in forum Windows Programming
    Replies: 4
    Last Post: 06-26-2005, 07:11 PM
  5. Method of placing child window controls
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 01-13-2002, 11:09 PM