using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace thread
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
object objlock = new object();
public void tick()
{
for (int i = 0; i < 10; i++)
{
lock (objlock)
{
textBox1.Text += "Tick"+Environment.NewLine;
Monitor.Pulse(objlock);
Monitor.Wait(objlock);
}
}
}
public void tock()
{
for (int i = 0; i < 10; i++)
{
lock (objlock)
{
textBox1.Text += "Tock";
Monitor.Pulse(objlock);
Monitor.Wait(objlock);
}
}
}
// public void toe()
// {
//
// for (int i = 0; i < 10; i++)
// {
// lock (objlock)
// {
// textBox1.Text += "Toe";
//
// Monitor.Pulse(objlock);
// Monitor.Wait(objlock);
// }
// }
// }
private void Form2_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(tick);
Thread th1 = new Thread(tock);
//Thread th2 = new Thread(toe);
th.Start();
th1.Start();
// th2.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace thread
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
object objlock = new object();
public void tick()
{
for (int i = 0; i < 10; i++)
{
lock (objlock)
{
textBox1.Text += "Tick"+Environment.NewLine;
Monitor.Pulse(objlock);
Monitor.Wait(objlock);
}
}
}
public void tock()
{
for (int i = 0; i < 10; i++)
{
lock (objlock)
{
textBox1.Text += "Tock";
Monitor.Pulse(objlock);
Monitor.Wait(objlock);
}
}
}
// public void toe()
// {
//
// for (int i = 0; i < 10; i++)
// {
// lock (objlock)
// {
// textBox1.Text += "Toe";
//
// Monitor.Pulse(objlock);
// Monitor.Wait(objlock);
// }
// }
// }
private void Form2_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(tick);
Thread th1 = new Thread(tock);
//Thread th2 = new Thread(toe);
th.Start();
th1.Start();
// th2.Start();
}
}
}