Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A timer func problem???

A timer func problem???

Scheduled Pinned Locked Moved C#
questiongraphicsdockerhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cemlouis
    wrote on last edited by
    #1

    Hi, I have the below code which runs after choosing an item from the listbox (choose the first one). Goes to the below item after 5 seconds and then goes to another after 5 sec again... So here is my question: I have a func which I named as process() I want to run this func only once when each item is selected in the listbox? So how can I do that??? Thank you, Cem Louis using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Timers; using System.Threading; namespace WindowsApplication14 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Timers.Timer timerClock = new System.Timers.Timer(); /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); InitializeTimer(); // // TODO: Add any constructor code after InitializeComponent call // } public void InitializeTimer() { this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer); this.timerClock.Interval = 5000; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(8, 112); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "Run"; this.button1.Click += new System.EventHandler(this.button1_Click); // // For

    P 1 Reply Last reply
    0
    • C cemlouis

      Hi, I have the below code which runs after choosing an item from the listbox (choose the first one). Goes to the below item after 5 seconds and then goes to another after 5 sec again... So here is my question: I have a func which I named as process() I want to run this func only once when each item is selected in the listbox? So how can I do that??? Thank you, Cem Louis using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Timers; using System.Threading; namespace WindowsApplication14 { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Timers.Timer timerClock = new System.Timers.Timer(); /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); InitializeTimer(); // // TODO: Add any constructor code after InitializeComponent call // } public void InitializeTimer() { this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer); this.timerClock.Interval = 5000; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(8, 112); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(120, 23); this.button1.TabIndex = 1; this.button1.Text = "Run"; this.button1.Click += new System.EventHandler(this.button1_Click); // // For

      P Offline
      P Offline
      partyganger
      wrote on last edited by
      #2

      Add a boolean class member that starts out as false. Whenever the timer event is triggered, check if it's false. If so, set it to true. In the ListBox SelectedIndexChanged event handler (or whatever the event is called that gets called whenever you select a different item from the listbox), set the boolean to false again, thus resetting it and allowing the timer event to run again. This way, Process() can only run when a different item has been selected. And since the Process() then sets the boolean flag to true, it will only run once. bool itemIsInTimer = false; private void listbox1_SelectedIndexChanged(object sender, System.EventArgs e) { this.itemIsInTimer = false; } private void process() { if(!this.itemIsInTimer) { this.itemIsInTimer = true; // do process } }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups