×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

C# Thread Basic Please correct this (Beginner)

C# Thread Basic Please correct this (Beginner)

C# Thread Basic Please correct this (Beginner)

(OP)
This does not get reflected in the Form.
I want somebody to simply correct this.

CODE --> C#

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();
        }


    }
} 

RE: C# Thread Basic Please correct this (Beginner)

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

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form\cf3
    {
        private System.Timers.Timer Timer1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.label1.Text = DateTime.Now.ToString("MM:HH:ss");
            StartTimer();
        }

        private void StartTimer()
        {
            Timer1 = new System.Timers.Timer();
            Timer1.Elapsed += new System.Timers.ElapsedEventHandler(Timer1_Tick);
            Timer1.Interval = 1000;
            Timer1.Enabled = true;
            Timer1.Start();

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<object, EventArgs>(Timer1_Tick), new object[] { sender, e });
            }
            this.label1.Text = DateTime.Now.ToString("MM:HH:ss");
        }

    }
}
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close