这是我的计时器代码:
private static System.Timers.Timer CSVTimer;
public static void CSVTimer_Start()
{
CSVTimer = new System.Timers.Timer();
CSVTimer.Interval = 10000;
CSVTimer.Elapsed += TCSVCreatEvent;
CSVTimer.AutoReset = true;
CSVTimer.Enabled = true;
}
public static string s = "";
private static void TCSVCreatEvent(Object source, System.Timers.ElapsedEventArgs e)
{
s += "[ADD0]";
DatabaseController DC = new DatabaseController();
string systempath = @"C:\inetpub\wwwroot\DiceGameServerPath\File\SuibaoFile";
string FileName = DateTime.Now.AddDays(-1).ToString("yyyy_MM_dd_HHmmss") + ".csv";
string text = "test0,test1,test2" + Environment.NewLine;
//text += DC.ReturnYesterdayData();
s += "[ADD1]";
if (!File.Exists(systempath + FileName))
{
s += "[ADD2]";
var NewFile = File.Create(systempath + FileName);
s += "[ADD3]";
NewFile.Close();
s += "[ADD4]";
FileStream fs = new FileStream(systempath + FileName, FileMode.Open, FileAccess.Write);
s += "[ADD5]";
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
s += "[ADD6]";
sw.Write(text);
s += "[ADD7]";
sw.Close();
s += "[ADD8]";
}
}
我尝试使用此代码编写一个.csv
文件,但是它不起作用。
我打印出s
以查看在哪里停止,我在“ [ADD2]”处停止了代码
然后我复制TCSVCreatEvent()
代码,并使用按钮调用此方法,效果很好
所以我不明白为什么相同的代码不能在计时器中工作?
-------------编辑-----------
代码停在
var NewFile = File.Create(systempath + FileName);