ive 프로그램에서 작업 중입니다. 나는 3 개의 클래스가 있습니다. 클래스 중 2 개는 서로 다른 간격으로 반복되는 타이머를 가지고 있으며 타이머의 "순환"이 완료되면 문자열을 반환하는 이벤트를 발생시킵니다. 세 번째 클래스는 다른 두 타이머 클래스의 이벤트를 구독하고 화면에 출력합니다. 그것은 위대한 작품!C#에서 이벤트 발생 및 출력?
하지만 내 문제는 개별적으로 인쇄한다는 것입니다. 현재 첫 번째 타이머 클래스가 실행되고 2 분마다 "hello"가 발생하고 매 초 다른 클래스 "dog"가 발생한다고 가정 해보십시오. 이벤트가 발생할 때마다 발생한 이벤트를 콘솔에 출력합니다. 나는 매초마다 "hellodog"를 인쇄하고 싶습니다.
나는 생각했다. 타이머가 작동 할 때마다 이벤트가 발생하고 "출력"클래스의 문자열을 현재 값으로 업데이트 한 다음 매초마다 다른 타이머를 만들고이 타이머는 "hellodog"와 같은 하나의 출력으로 문자열을 업데이트했습니다. 이게 내가 생각하는 가장 쉬운 방법이라면 가능하다. 어떻게이 아이디어를 얻을 수 있을까요?
혼란 스러울 경우 명확히 할 것입니다.
namespace Final
{
public class Output
{
public static void Main()
{
var timer1 = new FormWithTimer();
var timer2 = new FormWithTimer2();
timer1.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer1_NewStringAvailable);
timer2.NewStringAvailable += new EventHandler<BaseClassThatCanRaiseEvent.StringEventArgs>(timer2_NewStringAvailable);
Console.ReadLine();
}
static void timer1_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e)
{
var theString = e.Value;
//To something with 'theString' that came from timer 1
Console.WriteLine(theString);
}
static void timer2_NewStringAvailable(object sender, BaseClassThatCanRaiseEvent.StringEventArgs e)
{
var theString2 = e.Value;
//To something with 'theString2' that came from timer 2
Console.WriteLine(theString2);
}
}
public abstract class BaseClassThatCanRaiseEvent
{
public class StringEventArgs : EventArgs
{
public StringEventArgs(string value)
{
Value = value;
}
public string Value { get; private set; }
}
//The event itself that people can subscribe to
public event EventHandler<StringEventArgs> NewStringAvailable;
protected void RaiseEvent(string value)
{
var e = NewStringAvailable;
if (e != null)
e(this, new StringEventArgs(value));
}
}
public partial class FormWithTimer : BaseClassThatCanRaiseEvent
{
Timer timer = new Timer();
public FormWithTimer()
{
timer = new System.Timers.Timer(200000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (200000); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
...
RaiseEvent(gml.ToString());
}
}
public partial class FormWithTimer2 : BaseClassThatCanRaiseEvent
{
Timer timer = new Timer();
public FormWithTimer2()
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Tick2); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick2(object sender, EventArgs e)
{
...
RaiseEvent(aida.ToString());
}
}
}
가능한 중복 http://stackoverflow.com/questions/7375452/how-do -i-subscribe-to-raised-events-and-printing-together) –
또한 http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed를 참조하십시오. -from-posts –
나는 이해합니다 – Csharpz