小编Mik*_*ike的帖子

System.IO.IOException:'该进程无法访问文件'@ .txt',因为它正在被另一个进程使用。

我是编程新手,但有一个问题。如果我有两个功能,一个创建一个文本文件并将其写入,另一个打开同一个文本文件并从中读取。

我得到的错误是:

System.IO.IOException:'该进程无法访问文件'@ .txt',因为它正在被另一个进程使用。

我尝试为每个功能设置单独的计时器,但仍然无法正常工作。我认为最好的方法是,直到功能一结束,功能二才开始。

你能帮我实现这个目标吗?非常感谢你!麦克风

源代码:

public Form1() {
    InitializeComponent();
    System.Timers.Timer timerButtona1 = new System.Timers.Timer();
    timerButtona1.Elapsed += new ElapsedEventHandler(tickTimera1);
    timerButtona1.Interval = 3003;
    timerButtona1.Enabled = true;
}

private async void tickTimera1(object source, ElapsedEventArgs e) {
    function1();
    function2();
}

void function1() {
    List<string> linki = new List<string>();

    linki.Add("https://link1.net/");
    linki.Add("https://link2.net/");
    linki.Add("https://link3.net/");

    List<string> fileNames = new List<string>();

    fileNames.Add("name1");
    fileNames.Add("name2");
    fileNames.Add("name3");

    for (int x = 0; x < fileNames.Count; x++) {
        GET(linki[x], fileNames[x]);
        //System.Threading.Thread.Sleep(6000);
    }
}

async void GET(string link, string fileName) {
    var …
Run Code Online (Sandbox Code Playgroud)

c# concurrency ioexception

6
推荐指数
4
解决办法
3万
查看次数

标签 统计

c# ×1

concurrency ×1

ioexception ×1