我刚开始学编程,有一个问题:如果我有两个函数,一个用于创建文本文件并将内容写入,而另一个用于打开同一文本文件并从中读取。 我遇到的错误是:
System.IO.IOException:“由于正在被另一个进程使用,因此无法访问文件'@.txt'。”
我尝试为每个函数设置单独的计时器,但仍然无法正常工作。 我认为最好的方法是在函数一结束之前不启动函数二。
你能帮我实现这个吗? 非常感谢! Mike 源代码:
System.IO.IOException:“由于正在被另一个进程使用,因此无法访问文件'@.txt'。”
我尝试为每个函数设置单独的计时器,但仍然无法正常工作。 我认为最好的方法是在函数一结束之前不启动函数二。
你能帮我实现这个吗? 非常感谢! Mike 源代码:
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 ODGOVOR = await PRENOS_PODATKOV.GetStringAsync(link);
File.WriteAllText(@"C:\Users\...\" + fileName + ".txt", ODGOVOR);
}
void function2() {
string originalText = File.ReadAllText(@"C:\Users\...\fileName.txt", Encoding.Default);
dynamic modifiedText = JsonConvert.DeserializeObject(originalText);
//then i then i read from the same text files and use some data from it..
}