Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Thread Basic Please correct this (Beginner)

Status
Not open for further replies.

atzsea

Programmer
Jun 1, 2005
49
0
0
JP
This does not get reflected in the Form.
I want somebody to simply correct this.

Code:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Timer Timer1;
        private System.Threading.Thread thr;
        public Form1()
        {
            InitializeComponent();

            Thread1();
        }

        private void Thread1()
        {
            thr = new System.Threading.Thread(new System.Threading.ThreadStart(threadmethod1));
            thr.Start();
        }

        private void threadmethod1()
        {
            Timer1 = new Timer();
            Timer1.Interval = 200;

            Timer1.Tick += new EventHandler(Timer1_Tick);

            Timer1.Enabled = true;
            Timer1.Start();
            
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            this.label1.Text = dt.ToLongTimeString();
        }


    }
}
 
For one thing, you don't need to run your timer in a separate thread; the timer already creates its own thread.. and you also want to use System.Timers.Timer instead. Generally speaking you won't have to get into directly threading things.. the only time I find myself ever directly creating a Thread is for BIG file operations.

You also need to make the update thread safe.



Code:
[COLOR=#0000FF]using[/color][COLOR=#000000] System;
[/color][COLOR=#0000FF]using[/color][COLOR=#000000] System.Windows.Forms;

[/color][COLOR=#0000FF]namespace[/color][COLOR=#000000] WindowsFormsApplication1
{
    [/color][COLOR=#0000FF]public[/color][COLOR=#000000] [/color][COLOR=#0000FF]partial[/color][COLOR=#000000] [/color][COLOR=#0000FF]class[/color][COLOR=#000000] [/color][COLOR=#2B91AF]Form1[/color][COLOR=#000000] : [/color][COLOR=#2B91AF]Form\cf3
    {
        [/color][COLOR=#0000FF]private[/color][COLOR=#000000] System.Timers.[/color][COLOR=#2B91AF]Timer[/color][COLOR=#000000] Timer1;

        [/color][COLOR=#0000FF]public[/color][COLOR=#000000] Form1()
        {
            InitializeComponent();
        }

        [/color][COLOR=#0000FF]private[/color][COLOR=#000000] [/color][COLOR=#0000FF]void[/color][COLOR=#000000] Form1_Load([/color][COLOR=#0000FF]object[/color][COLOR=#000000] sender, [/color][COLOR=#2B91AF]EventArgs[/color][COLOR=#000000] e)
        {
            [/color][COLOR=#0000FF]this[/color][COLOR=#000000].label1.Text = [/color][COLOR=#2B91AF]DateTime[/color][COLOR=#000000].Now.ToString([/color][COLOR=#A31515]"MM:HH:ss"[/color][COLOR=#000000]);
            StartTimer();
        }

        [/color][COLOR=#0000FF]private[/color][COLOR=#000000] [/color][COLOR=#0000FF]void[/color][COLOR=#000000] StartTimer()
        {
            Timer1 = [/color][COLOR=#0000FF]new[/color][COLOR=#000000] System.Timers.[/color][COLOR=#2B91AF]Timer[/color][COLOR=#000000]();
            Timer1.Elapsed += [/color][COLOR=#0000FF]new[/color][COLOR=#000000] System.Timers.[/color][COLOR=#2B91AF]ElapsedEventHandler[/color][COLOR=#000000](Timer1_Tick);
            Timer1.Interval = 1000;
            Timer1.Enabled = [/color][COLOR=#0000FF]true[/color][COLOR=#000000];
            Timer1.Start();

        }

        [/color][COLOR=#0000FF]private[/color][COLOR=#000000] [/color][COLOR=#0000FF]void[/color][COLOR=#000000] Timer1_Tick([/color][COLOR=#0000FF]object[/color][COLOR=#000000] sender, [/color][COLOR=#2B91AF]EventArgs[/color][COLOR=#000000] e)
        {
            [/color][COLOR=#0000FF]if[/color][COLOR=#000000] (InvokeRequired)
            {
                [/color][COLOR=#0000FF]this[/color][COLOR=#000000].Invoke([/color][COLOR=#0000FF]new[/color][COLOR=#000000] [/color][COLOR=#2B91AF]Action[/color][COLOR=#000000]<[/color][COLOR=#0000FF]object[/color][COLOR=#000000], [/color][COLOR=#2B91AF]EventArgs[/color][COLOR=#000000]>(Timer1_Tick), [/color][COLOR=#0000FF]new[/color][COLOR=#000000] [/color][COLOR=#0000FF]object[/color][COLOR=#000000][] { sender, e });
            }
            [/color][COLOR=#0000FF]this[/color][COLOR=#000000].label1.Text = [/color][COLOR=#2B91AF]DateTime[/color][COLOR=#000000].Now.ToString([/color][COLOR=#A31515]"MM:HH:ss"[/color][COLOR=#000000]);
        }

    }
}
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top