Подписка" на event

R

rapa

Народ подскажите такую тему.У меня есть 2 класса.Oдин из них-это Form1 второй-MyTime.В MyTime есть event который происходит,когда меняется время,т.е. каждую секунду.Я запускаю этот event в методе,в котором есть бесконечный цикл(этот метод статический).
Как мне отразить изменение времени в классе Form1?
P.S.В нём,соответственно,есть "подписка" на event.

код:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace hw2_clock
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label HourLabel;
private System.Windows.Forms.Label MinuteLabel;
private System.Windows.Forms.Label SecondLabel;
private System.ComponentModel.IContainer components;
private MyTime mt = new MyTime();

public Form1()
{
InitializeComponent();
MyTime.OnTimeChanged += new MyTime.TimeChangeHandler(this.EventHandler);
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.HourLabel = new System.Windows.Forms.Label();
this.MinuteLabel = new System.Windows.Forms.Label();
this.SecondLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// HourLabel
//
this.HourLabel.Location = new System.Drawing.Point(24, 24);
this.HourLabel.Name = "HourLabel";
this.HourLabel.Size = new System.Drawing.Size(32, 23);
this.HourLabel.TabIndex = 0;
this.HourLabel.Text = "12";
//
// MinuteLabel
//
this.MinuteLabel.Location = new System.Drawing.Point(72, 24);
this.MinuteLabel.Name = "MinuteLabel";
this.MinuteLabel.Size = new System.Drawing.Size(24, 23);
this.MinuteLabel.TabIndex = 1;
this.MinuteLabel.Text = "29";
//
// SecondLabel
//
this.SecondLabel.Location = new System.Drawing.Point(112, 24);
this.SecondLabel.Name = "SecondLabel";
this.SecondLabel.Size = new System.Drawing.Size(32, 23);
this.SecondLabel.TabIndex = 2;
this.SecondLabel.Text = "19";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.SecondLabel);
this.Controls.Add(this.MinuteLabel);
this.Controls.Add(this.HourLabel);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void EventHandler(int hour,int minute,int second)
{
HourLabel.Text = hour.ToString();
MinuteLabel.Text = minute.ToString();
SecondLabel.Text = second.ToString();
}

private void Form1_Load(object sender, System.EventArgs e)
{
HourLabel.Text = DateTime.Now.Hour.ToString();
MinuteLabel.Text = DateTime.Now.Minute.ToString();
SecondLabel.Text = DateTime.Now.Second.ToString();
//mt.Run();
}
}

#region Class MyTime
public class MyTime
{
public MyTime()
{
DateTime dt = DateTime.Now;
hour = dt.Hour;
minute = dt.Minute;
second = dt.Second;
}
private int hour,minute,second;
public int Hour
{
get{return hour;}
set{hour = value;}
}
public int Minute
{
get{return minute;}
set{minute = value;}
}
public int Second
{
get{return second;}
set{second = value;}
}
public delegate void TimeChangeHandler(int h,int m,int s);
public static event TimeChangeHandler OnTimeChanged;
public void Run()
{
while(true)
{
Thread.Sleep(1000);
DateTime dt = DateTime.Now;
if(Second != dt.Second)
{
if(OnTimeChanged != null)
{
OnTimeChanged(dt.Hour,dt.Minute,dt.Second);
}
}
}
}
}
#endregion
}
 

waldo

Местный
Регистрация
22 Фев 2004
Сообщения
210
Реакции
59
Credits
0
Если я правильно понял твой код, то MyTime надо запускать в другом потоке.
Либо (если я правильно помню) в Шарпе есть аналог WinApi-шного WM_TIME: сетапишь его чтобы он раз в какое-то время сам дергал у тебя метод (кидал тебе event), но подробностей не скажу - на шарпе почти не писал.