多进程读取和放大器;写一个文件 [英] Multi processes read&write one file

查看:198
本文介绍了多进程读取和放大器;写一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这将是读取和多进程写了一个txt文件的abc.txt。所以,当一个过程被读取或写入到文件的abc.txt,将文件abc.txt必须被锁定,使得任何其它进程不能读取或写入到它。我知道枚举System.IO.FileShare可能是解决这个问题的正确方法。但我用另一种方式,我不知道,如果它是正确的。以下是我的解决方案。

I have a txt file ABC.txt which will be read and wrote by multi processes. So when one process is reading from or writing to file ABC.txt, file ABC.txt must be locked so that any other processes can not reading from or writing to it. I know the enum System.IO.FileShare may be the right way to handle this problem. But I used another way which I'm not sure if it is right. The following is my solution.

我添加了另一个文件Lock.txt的文件夹。之前,我可以读取或写入文件的abc.txt,我必须从文件Lock.txt阅读的能力。而我从读出或写入将文件abc.txt后,我不得不释放能力。以下是code。

I added another file Lock.txt to the folder. Before I can read from or write to file ABC.txt, I must have the capability to read from file Lock.txt. And after I have read from or written to file ABC.txt, I have to release that capability. The following is the code.

        #region Enter the lock
        FileStream lockFileStream = null;
        bool lockEntered = false;
        while (lockEntered == false)
        {
            try
            {
                lockFileStream = File.Open("Lock.txt", FileMode.Open, FileAccess.Read, FileShare.None);
                lockEntered = true;
            }
            catch (Exception)
            {
                Thread.Sleep(500);
            }
        }
        #endregion

        #region Do the work
        // Read from or write to File ABC.txt
        // Read from or write to other files
        #endregion

        #region Release the lock
        try
        {
            if (lockFileStream != null)
            {
                lockFileStream.Dispose();
            }
        }
        catch
        {
        }
        #endregion

在我的电脑上,似乎这个解决方案工作得很好,但我现在还不能确定它是否是合适的。

On my computer, it seems that this solution works well, but I still can not make sure if it is appropriate..

编辑:多进程,不是多线程在同一进程

Multi processes, not multi threads in the same process.

(对不起,我英文不好)

(Sorry for my poor English)

推荐答案

C#的名为<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx\">EventWaitHandle是去这里的道路。在每艘要使用该文件,并给它是由所有这些进程共享的名称流程创建等待句柄的一个实例。

C#'s named EventWaitHandle is the way to go here. Create an instance of wait handle in every process which wants to use that file and give it a name which is shared by all such processes.

EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, "SHARED_BY_ALL_PROCESSES");

访问对的WaitHandle 文件等待,完成后处理文件时

然后,将其设置所以在队列中的下一个进程可能会访问它。

Then when accessing the file wait on waitHandle and when finished processing file, set it so the next process in the queue may access it.

waitHandle.WaitOne();
/* process file*/
waitHandle.Set();

当你命名事件等待句柄,然后该名称是共享的所有进程在操作系统中。因此,为了避免碰撞的可能,使用GUID的名称(SHARED_BY_ALL_PROCESSES以上)。

When you name an event wait handle then that name is shared across all processes in the operating system. Therefore in order to avoid possibility of collisions, use a guid for name ("SHARED_BY_ALL_PROCESSES" above).

这篇关于多进程读取和放大器;写一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆