超级简单的:
//声明委托
public delegate void DeleMethod();
class Program
{
static void Main(string[] args)
{
DeleMethod dele = CreatTxt;
IAsyncResult result = dele.BeginInvoke(null,null);
dele.EndInvoke(result);
using (StreamWriter sw = new StreamWriter(@"D:\test1.txt"))
{
string temp = "Finsih";
sw.Write(temp);
}
}
//创建文本方法
static void CreatTxt()
{
FileStream filesr = File.Create(@"D:\test1.txt");
filesr.Close();
Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
给你个简单的示例看下,一个线程等待另一个线程结束了才执行的。
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
static System.Threading.EventWaitHandle wh = new System.Threading.AutoResetEvent(false);
#region 下面两个按钮开启两个线程
private void button1_Click(object sender, EventArgs e)
{
new System.Threading.Thread(CreateFile).Start();
}
private void button2_Click(object sender, EventArgs e)
{
new System.Threading.Thread(WriteFinishedText).Start();
}
#endregion
private void CreateFile()
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine("CreateFile");
wh.Set();
}
private void WriteFinishedText()
{
wh.WaitOne();
Console.WriteLine("Finished");
}
}
我把sleep的时间弄成20秒来测试的,点了btn1后没有反应是正常的,可是点了btn2后它不会等btn1执行完成,而是直接输出。。。我把修改的代码放上来。。麻烦你帮我看看是怎么回事。。谢谢
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;
namespace WinTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static System.Threading.EventWaitHandle wh = new System.Threading.AutoResetEvent(false);
System.IO.FileInfo file = new System.IO.FileInfo(@"D:\ThreadText.txt");
#region 下面两个按钮开启两个线程
private void button1_Click(object sender, EventArgs e)
{
new System.Threading.Thread(CreateFile).Start();
}
private void button2_Click(object sender, EventArgs e)
{
new System.Threading.Thread(WriteFinishedText).Start();
}
#endregion
private void CreateFile()
{
System.Threading.Thread.Sleep(20000);
System.IO.File.AppendAllText(@"D:\ThreadTest.txt", "Started..");
wh.Set();
}
private void WriteFinishedText()
{
wh.WaitOne();
System.IO.File.AppendAllText(@"D:\ThreadTest.txt", "Ended..");
}
}
}
@hexllo: 我用你的代码测了也是可以的啊,你看
ThreadTest.txt文件中写的内容就知道了,如下:
Started..Ended..Started..Ended..
表明都是先执行方法CreateFile再执行的方法WriteFinishedText
@LCM: 顺序是这样的,但是你点了按钮1后,立即点按钮2,这时你再打开..看到里面已经写入"Finished"了,而没有等按钮1的代码执行完..
@hexllo: 不会的啦,因为按钮2执行的方法刚执行的时候就是wh.WaitOne();,就是说没有wh.Set它不执行下去的。
建立那个线程,第一个线程写文件,第二个线程一直检测第一个线程是否已完成;如果完成再执行第二个线程的方法;
用下面的这个属性得到第一个是否已完成;
IsAlive | 获取一个值,该值指示当前线程的执行状态。 |
static object o = new object();
static void Main()
{
Thread creatth = new Thread(new ThreadStart(Create));
Thread writeth = new Thread(new ThreadStart(Write));
File.Delete("test.txt");
writeth.Start();
creatth.Start();
Console.ReadKey();
}
public static void Create()
{
FileStream text = File.Create("test.txt");
lock (o)
{
Console.WriteLine("文件建立");
Thread.Sleep(10000);
text.Close();
}
}
public static void Write()
{
FileStream text = null;
while (text == null)
{
try
{
text = File.Open("test.txt", FileMode.Open);
}
catch
{
Thread.Sleep(1000);
}
}
lock (o)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes("End");
text.Write(bytes, 0, bytes.Length);
text.Flush();
Console.WriteLine("写入成功");
text.Close();
}
}
编辑器加载中...