Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:38:47+00:00 2026-06-15T07:38:47+00:00

I am using a timer to output text to textbox every 2 seconds. but

  • 0

I am using a timer to output text to textbox every 2 seconds. but it seems that it doesnt work. any idea what is wrong. here is my code:

public partial class Form1 : Form
{
    public Form1()
    {
      InitializeComponent();
    }

    public static System.Timers.Timer aTimer;

    public void BtnGenData_Click(object sender, EventArgs e)
    {
      aTimer = new System.Timers.Timer(10000);

      // Hook up the Elapsed event for the timer.
      aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

      // Set the Interval to 2 seconds (2000 milliseconds).
      aTimer.Interval = 2000;
      aTimer.Enabled = true;
    }

    public static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
      string GenData = "Welcome";
      Form1 frm1 = new Form1();
      frm1.TboxData.AppendText(GenData.ToString());
    }
}

Actually i dont see any output coming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:38:48+00:00Added an answer on June 15, 2026 at 7:38 am

    Although this is not straightly connected with the problem you have in your code, but…

    From MSDN System.Timers.Timer:

    The server-based Timer is designed for use with worker threads in a
    multithreaded environment.

    In Windows Forms you can use System.WindowsForms.Timer:

        System.Windows.Forms.Timer timer;
    
        public Form1()
        {
            InitializeComponent();
    
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
        }
    
        public void BtnGenData_Click(object sender, EventArgs e)
        {
            BtnGenData.Enabled = false;
            timer.Start();
        }
    
        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            BtnGenData.Enabled = true;
    
            //do what you need
        }
    

    As for your code, why make the timer static? Try to use this:

     public partial class Form1 : Form
     {
        public Form1()
        {
            InitializeComponent();
        }
    
        public System.Timers.Timer aTimer;
    
        public void BtnGenData_Click(object sender, EventArgs e)
        {
            aTimer = new System.Timers.Timer(10000);
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 2000;
            aTimer.Enabled = true;
        }
    
         public void OnTimedEvent(object source, ElapsedEventArgs e)
         {
            this.TboxData.AppendText("Welcome");
         } 
     }
    

    Also you should take into consideration, what could happen if you pressed the button twice…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using timer(triggers every 2 hours) to check for any update in my
i need to loop using timer and want to display the output, but the
I can output a locale sensitive time format using strftime('%X') , but this always
I'm updating a picture by using timer, but it causes fatal performance problem. Then
I am using a timer function in Flex but it gives me a run
I have built a library that dumps most of its debug text using Console.WriteLine();
I have a timer that loops through following code every second. this code reads
I'm having a hard time enabling git colored output on windows when using console2.
t = Time.now d = 10.minutes.from_now Using these two variables, how do I output
Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.