このプロセスには2つのスレッドがあります。ボタン (開始、一時停止、一時停止、再開) を持つフォーム。アプリケーション全体を使用して一時停止するたびにEWH.WaitOne()
フリーズ (一時停止) し、再開ボタンを押すことができなくなります
フォームの実行中にのみ 2 つのスレッドを一時停止する方法はありますか? (私のコードのスレッド 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;
}