我有这个过程,其中有两个线程。和一个带有按钮(开始、暂停、暂停、恢复)的表单。每当我暂停使用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;
}