이 프로세스에는 두 개의 스레드가 있습니다. 단추가있는 양식 (시작, 일시 중단, 일시 중지, 다시 시작)이 있습니다. EWH.WaitOne()
을 사용하여 일시 중지 할 때 전체 응용 프로그램이 멈추고 더 이상 다시 시작 버튼을 누르지 못합니다.EventWaitHandler를 사용하여 특정 스레드를 일시 중단하는 방법
양식이 계속 실행되는 동안 만 일시 중단 할 수있는 방법이 있습니까? 직접 스레드를 일시 중단 (내 코드에서 스레드 1, 2)
public partial class Form1 : Form
{
public static System.Timers.Timer timer;
static Thread Thread1;
static Thread Thread2;
private static EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
using (var writer = File.AppendText("WriteTo.txt"))
{
writer.AutoFlush = true;
writer.WriteLine(e.SignalTime);
}
}
static void method1()
{
string text = "";
using (FileStream fs = File.Open("C:\\Users\\Wissam\\Documents\\Visual Studio 2010\\Projects\\Proc1\\Proc1\\bin\\Debug\\MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
int length = (int)fs.Length;
byte[] b = new byte[length];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
text += temp.GetString(b);
}
using (FileStream fs1 = File.Open("C:\\Users\\Wissam\\Documents\\Visual Studio 2010\\Projects\\Proc1\\Proc1\\bin\\Debug\\MyFile1.txt", FileMode.Open, FileAccess.Write, FileShare.None))
{
fs1.Write(temp.GetBytes(text), 0, temp.GetByteCount(text));
Thread.Sleep(1000);
}
}
}
static void method2()
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1000;
timer.Enabled = true;
}
다른 스레드를 작성하는 이유는 무엇입니까? 여기서 달성하고자하는 것이 무엇인지 분명하지 않습니다. 나는 WaitOne()이 당신이 생각하는 것을하지 않는다고 생각한다. –
난 그냥 여러 스레드를 사용해야합니다, ther 기능에 대해 걱정하지 마십시오 –
예 그 의심. 하지만 파일에 쓰는 것을 멈추게 할 thread.suspend()와 비슷한 것이 필요합니다. –