首页 新闻 赞助 找找看

C# 使用多线程实现这个简单功能,求代码~~

0
悬赏园豆:5 [已解决问题] 解决于 2012-08-21 14:21

请给我段C#代码,在A线程中建立一个文件D:\x.txt后强制占用它10秒,B线程必须等A线程解除占用后,往x.txt写入"Finished",谢谢了!

hexllo的主页 hexllo | 菜鸟二级 | 园豆:318
提问于:2012-01-04 13:04
< >
分享
最佳答案
0

超级简单的:

//声明委托
    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));
        }
    }

收获园豆:3
Anleb | 菜鸟二级 |园豆:208 | 2012-02-11 17:32
其他回答(3)
0

给你个简单的示例看下,一个线程等待另一个线程结束了才执行的。

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");
}
}
收获园豆:2
LCM | 园豆:6876 (大侠五级) | 2012-01-04 13:28

我把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..");
}
}
}
支持(0) 反对(0) hexllo | 园豆:318 (菜鸟二级) | 2012-01-04 14:17

@hexllo: 我用你的代码测了也是可以的啊,你看

ThreadTest.txt文件中写的内容就知道了,如下:
Started..Ended..Started..Ended..


表明都是先执行方法CreateFile再执行的方法WriteFinishedText
支持(0) 反对(0) LCM | 园豆:6876 (大侠五级) | 2012-01-04 14:30

@LCM: 顺序是这样的,但是你点了按钮1后,立即点按钮2,这时你再打开..看到里面已经写入"Finished"了,而没有等按钮1的代码执行完..

支持(0) 反对(0) hexllo | 园豆:318 (菜鸟二级) | 2012-01-11 15:13

@hexllo: 不会的啦,因为按钮2执行的方法刚执行的时候就是wh.WaitOne();,就是说没有wh.Set它不执行下去的。

支持(0) 反对(0) LCM | 园豆:6876 (大侠五级) | 2012-01-11 15:24
0

建立那个线程,第一个线程写文件,第二个线程一直检测第一个线程是否已完成;如果完成再执行第二个线程的方法;

用下面的这个属性得到第一个是否已完成;

IsAlive 获取一个值,该值指示当前线程的执行状态。
画方软件 | 园豆:778 (小虾三级) | 2012-01-04 14:56
0
 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();
}
}


编辑器加载中...

Rookier | 园豆:652 (小虾三级) | 2012-01-17 10:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册