I have this process with two threads in it. and a form that has buttons (start, suspend, pause, resume). whenever I suspend using the EWH.WaitOne()
the whole application freezes (suspends) and I can't press the resume button any more
Is there a way to suspend the 2 threads only while the form keeps on running? (thread 1 and 2 in my code)
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;
}