C# Threading & Csharp Forms.Timer

Download Code and Test Application for System.Windows.Forms.Timer
The Windows.Forms.Timer raises the event back on the UI thread so you can update the UI from the event handlers directly.

An advantage of using Windows.Forms timers is that it’s very easy to control the rate of GUI updates and thus performance.

it is a sample test application screenshot.
Also you can download C# code.

System.Windows.Forms.Timer Test Application in Csharp

Thread 1 printing even values at every one seconds and
Thread 2 printing odd values at every two seconds. Our two threads work with together at the same time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
namespace threadApplication
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        System.Windows.Forms.Timer t2 = new System.Windows.Forms.Timer();
        int i = 0;
        int j = 1;
        public Form1()
        {
            InitializeComponent();
        }
 
        void ThreadFuncEven()
        {
            output.Text += 
            "\nThread1 interval = 1 Seconds. Even Value=" + i.ToString();
            i += 2;
            output.Refresh();
        }
        void ThreadFuncOdd()
        {
            output.Text += 
            "\nThread2 interval = 2 Seconds. Odd Value=" + j.ToString();
            j += 2;
            output.Refresh();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            t.Interval = 1000;
            t.Start();
            t.Tick += delegate(object _s, EventArgs _e)
            {
                ThreadFuncEven();
            };
 
            t2.Interval = 2000;
            t2.Start();
            t2.Tick += delegate(object _s, EventArgs _e)
            {
                ThreadFuncOdd();
            };
        }
    }
}


Views (4611)

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

SoftCodeArticle was formed to help developers solve common software issues.