/*
 * BackgroundWorker 사용법 : Form 혹은 Console이 사라지면 함께 작동을 멈춘다.
 * BackgroundWorker 클래스
   네임스페이스: System.ComponentModel
 */


using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

public class Program : Form
{
int toggle=0;

public Program()
{
this.Text="BackgroungWorker Thread Process!!";
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
         switch (keyData)
         {
         case Keys.S:
             break; 
         case Keys.F5:
             break; 
         case Keys.Enter:
             Application.Exit();
             break;
         case Keys.Tab:
             break;
         default:
             break;
         }
     }
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

try
{
switch(m.Msg)
{
default:
if(toggle==0)
{
BackgroundMakeCls nm = new BackgroundMakeCls();
nm.running();

toggle=100; //Form이 실행시에 오직 한번만 수행하도록 한다.
}
break;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
}

public class BackgroundMakeCls
{
BackgroundWorker recvWorker = new BackgroundWorker();
BackgroundWorker sendWorker = new BackgroundWorker();

public void running()
{
recvWorker.DoWork += new DoWorkEventHandler(recvWorker_DoWork);
recvWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(recvWorker_RunWorkerCompleted);

sendWorker.DoWork += new DoWorkEventHandler(sendWorker_DoWork);
sendWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(sendWorker_RunWorkerCompleted);

recvWorker.RunWorkerAsync();
}
void recvWorker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine(">>>>recvWorker_DoWork");
}
void recvWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
Console.WriteLine(">>>>recvWorker_RunWorkerCompleted");

try
{
if(! sendWorker.IsBusy) sendWorker.RunWorkerAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void sendWorker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine(">>>>recvWorker_DoWork");
}
void sendWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine(">>>>recvWorker_RunWorkerCompleted");

try
{
if(! recvWorker.IsBusy) recvWorker.RunWorkerAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

+ Recent posts