在一个线程中,一个文件被不断地打开、关闭和处理。这会引起问题吗?
这是代码
StreamWriter file1 = new StreamWriter(filepath4, true);
for (int i = 0; i < ChannelValueForTcp; i++)
{
file1.WriteLine(data[i]);
}
file1.WriteLine(data[data.Length-1]);
file1.WriteLine(data[data.Length - 2]);
file1.Close();
file1.Dispose();
请帮助我卡住了。(这是随机出现的,我们试图连续运行代码 8 小时。)
编辑:
没有其他线程工作或做任何与此文件相关的事情。它只在这里使用。还有其他线程正在运行,它们给出了相同的错误,但在 45 分钟 - 5 小时的测试后随机出现。
[DllImport("ConsoleApplication2.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int main_c();
public string[] tcp(string peer, int port)
{
int i = main_c();//the c code writes to a file called akash.txt and returns = 0 if it is successful. Then I read the file and do some functions on it.
if (i == 0)
{
StreamReader objReader = new StreamReader("akash.txt");