뭐 정도 일이면 숫작세는 것을 윈폼에서 하거나 윈폼의 타이머 이벤트를 이용하는게 조금 더 깔끔할 거 같음.
분리를 해야할 때....
카운터쪽
using System.Threading;
using System.Windows.Forms;
namespace AsyncCount
{
public delegate void Blidge(int i);
public class Counter
{
private int cnt = 1;
public Thread thread;
public Blidge Update;
public int Count
{
set
{
cnt = value;
}
get
{
return cnt;
}
}
public Counter()
{
thread = new Thread(new ThreadStart(count));
Status = true;
thread.Start();
}
public void count()
{
while (Status)
{
Thread.Sleep(1000);
cnt = cnt + 1;
Update(cnt);
}
}
public bool Status
{
set;
get;
}
public static void Main()
{
Application.Run( new Form1(new Counter()) );
}
}
}
윈폼쪽
using System.Windows.Forms;
namespace AsyncCount
{
public partial class Form1 : Form
{
private delegate void EventHandler_Count(int count);
EventHandler_Count eventandler;
Counter counter;
public Form1(Counter counter)
{
InitializeComponent();
counter.Update = delegate(int i)
{
try
{
Invoke(eventandler, i);
}
catch
{
counter.thread.Abort();
}
};
eventandler = new EventHandler_Count(delegate(int i)
{
label1.Text = i.ToString();
});
this.counter = counter;
eventandler.Invoke(counter.Count);
}
}
}