正常运行解决方案时,我只会收到System.IndexOutOfRangeException错误,但进入整个循环时一切正常。
我已经尝试了捕获异常,但是没有喜悦。
An error has occurred
The application failed to start (exited with code 1).
Attaching package: ‘DT’
The following objects are masked from ‘package:shiny’:
dataTableOutput, renderDataTable
Adding cronjob:
---------------
# cronR job
# id: temp_data_fetch
# tags: lab, xyz
# desc: temp Data Loading
0-59 * * * * /opt/R/3.5.3/lib/R/bin/Rscript '/srv/connect/apps/Temp/ETL.R' >> '/srv/connect/apps/Temp/ETL.log' 2>&1
Error in value[[3L]](cond) : error in running command
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
#Cron Script
if(!(file.exists("/srv/connect/apps/Temp/scripts_scheduled.rds"))){
cmd <- cronR::cron_rscript(rscript = '/srv/connect/apps/Temp/ETL.R')
cronR::cron_add(cmd, frequency = 'minutely', id = 'temp_data_fetch',description = 'temp Data Loading',tags = c('lab', 'xyz'))
TEXT<-"temp_data_fetch"
saveRDS(TEXT,"/srv/connect/apps/Temp/scripts_scheduled.rds")
}
#ETL.R
trigger_time <- readRDS(file = "/srv/connect/apps/Temp/trigger_time.rds")
trigger_time <- c(trigger_time,paste(Sys.time()))
saveRDS(trigger_time,file = "/srv/connect/apps/Temp/trigger_time.rds")
如果Jobs []。JobID为false,我希望初始化thread []数组中的所有线程。
下面是CountUp()方法:
private void button1_Click(object sender, EventArgs e)
{
for (int j = 0; j < jobs.Length; j++)
{
if (jobs[j].JobID == false)
{
for (int k = 0; k < threads.Length; k++)
{
if (threads[k] != null)
{
if (!(threads[k].ThreadState == ThreadState.Stopped) | !(threads[k].ThreadState == ThreadState.Unstarted))
{
continue;
}
}
try
{
threads[k] = new Thread(() => CountUp("ftp://ftp.net" + jobs[j].FTPFolder, HomePath + jobs[j].localFolder, j));
threads[k].Name = "Thread " + j + "¦ ID: " + threads[k].ManagedThreadId.ToString();
jobs[j].JobID = true;
//threads[k].Start();
break;
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
}
}
StartThreads();
}
以下初始化线程[]和作业[]:
private void CountUp(string ftppath,string localFile, int jobsID)
{
//string conf="";
NumberThreads++;
//string ftpPath = "ftp://ftp.Rxsystems.net" + conf.Split('¦')[1];
//string downloadPath = HomePath + conf.Split('¦')[0] + "\\";
string ftpPath = ftppath;
string downloadPath = localFile;
List<string> MSI = new List<string>(KD.FTP.Class.ListFiles(ftpPath,
FTPuser, FTPpass));
if (MSI.Count > 0)
{
KD.File.Class.Logger(Thread.CurrentThread.Name + ", " + MSI.Count + " Files in " + ftpPath, CurDir + "\\log.txt");
this.textBox1.AppendText(Thread.CurrentThread.Name + ", " + MSI.Count + " Files in " + ftpPath);
//this.textBox1.AppendText("\n\r");
int count = 0;
foreach (string ftpFile in MSI)
{
KD.FTP.Class.Download(ftpPath + ftpFile,downloadPath + "\\" + ftpFile, FTPuser,FTPpass);
count++;
KD.File.Class.Logger(Thread.CurrentThread.Name + ", " + "Downloaded " + count + "/" + MSI.Count + " Files - " + ftpFile, CurDir + "\\log.txt");
this.textBox1.AppendText(Thread.CurrentThread.Name + ", " + "Downloaded " + count + "/" + MSI.Count + " Files - " + ftpFile);
//this.textBox1.AppendText("\n\r");
}
}
NumberThreads--;
jobs[jobsID].JobID = false;
}
答案 0 :(得分:2)
从我可以看到的问题是j
变量,该变量从闭包捕获到传递给new Thread
的委托中。众所周知的问题是,实际的委托执行在循环执行后引用状态变量,因此应该有效地包含超出范围的jobs.Length
值。要解决此问题,您需要在循环内引入一个局部变量以复制j
的值,然后使用此变量代替j
作为传递给{{ 1}}构造函数:
jobs