我有一个C#应用程序,它引用另一个类来加载excel数据,如下所示:
这是Excel加载数据类:
public class LoadExcelDictionary
{
private Log log = new Log();
public List<ULTLData> Load(string Input,string OutputDirectory, BackgroundWorker BackgroundWorker)
{
List<ULTLData> output = new List<ULTLData>();
try
{
var segmentFull = 0;
XSSFWorkbook xssfWorkbook;
using (FileStream file = new FileStream(@Input, FileMode.Open, FileAccess.Read))
{
//hssfWorkbook = new HSSFWorkbook(file);
xssfWorkbook = new XSSFWorkbook(file);
}
ISheet sheet = xssfWorkbook.GetSheet("Sheet1");
DataFormatter df = new DataFormatter();
for (int i = 0; i <= sheet.LastRowNum; i++)
{
segmentFull = (((i + 1) * 100) / sheet.LastRowNum);
//0 is the header row
if (i > 0)
{
ULTLData uLTLData = new ULTLData
{
VV = df.FormatCellValue(sheet.GetRow(i).GetCell(0)),
};
output.Add(uLTLData);
Console.WriteLine("Loading Excel Data - " + uLTLData.VV);
}
BackgroundWorker.ReportProgress(segmentFull);
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
log.Output(e.Message + " " + e.Source);
}
return output;
}
}
我从UI代码中引用Excel数据加载器,如下所示:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var backgroundWorker = sender as BackgroundWorker;
segmentFull = 0;
LoadData ld = new LoadData();
LoadExcelDictionary led = new LoadExcelDictionary();
WriteFile wf = new WriteFile();
try
{
var loadedExcel = led.Load(Dictionary, Output, backgroundWorker);
setStatusLabel("Loading Excel Data Completed");
log.Output("Loading Excel Data Completed");
}
catch (Exception exception)
{
Console.WriteLine("Exception: " + exception.Message);
log.Output(exception.Message + " " + exception.Source);
}
}
问题是当我在调试中运行它时,程序运行良好,并在LoadExcelDictionary返回数据后继续运行。在发布模式下,它什么都不做,并保留在此行:
var loadingExcel = led.Load(Dictionary,Output,backgroundWorker);
我不确定该如何解决。
编辑:
我发现了问题所在。它与更新标签有关。我有使用的标签功能:
private void setStatusLabel(string Input)
{
if (statusLB.InvokeRequired)
statusLB.Invoke(new Action(() => statusLB.Text = Input));
else
statusLB.Text = Input;
}
这是我的应用程序突然停止的原因。这是动态更新标签的正确方法吗?