【发布时间】:2019-09-16 09:27:54
【问题描述】:
我有一个 Windows 应用程序,在 MainWindow.xaml.cs 中我有一个按钮单击选项要导入,它会根据拖入文本框中的电子表格来运行 Main.Run:
private void Btn_Import_Click(object sender, RoutedEventArgs e)
{
Main.Run(Global.bindingObjects.spreadsheetTxtBxFile);
}
上面的代码将我们带到一个任务,然后它应该去运行 DefunctRun 在另一个项目中,但是在调试中使用断点和 F10 时它不会去它:
internal static void Run(string spreadsheetpath)
{
Task task = new Task(
() =>
{
try
{
PICSObjects.DefunctFields.DefunctRun(spreadsheetpath);
}
finally
{
}
}
);
task.Start();
}
它应该启动并执行的代码,我需要将电子表格路径转换为数据集,如果可以访问该类应该执行该数据集。:
using InfExcelExtension;
using System.Data;
using System.IO;
using System.Diagnostics;
namespace PICSObjects
{
public static partial class DefunctFields
{
public static void DefunctRun(string spreadsheetpath)
{
//Sets up string values to be used later//
DataSet origdata = new DataSet();
DataSet newdata = new DataSet();
string filespath = @"";
string output = @".xlsx".ToString();
string searchtext = "";
string currentscript = "";
string readscript = "";
//Converts the above Path string (which should be a spreadsheet) into a dataset with
datatables//
origdata = ExcelToDataset.ToDataSet(spreadsheetpath);
//Sets up two new tables in new dataset//
newdata.Tables.Add("Scripts");
newdata.Tables.Add("Tables");
//Add columns to the new tables//
newdata.Tables["Scripts"].Columns.Add("ToRemove");
newdata.Tables["Scripts"].Columns.Add("ScriptName");
newdata.Tables["Scripts"].Columns.Add("RelatedTable");
newdata.Tables["Tables"].Columns.Add("TableName");
newdata.Tables["Tables"].Columns.Add("ScriptName");
//Sets the directory to browse in from the filespath specified at the top//
DirectoryInfo d = new DirectoryInfo(filespath.ToString());
//Goes through each file in specified directory that has .sql as the extension//
foreach (var file in d.GetFiles("*.sql"))
{
currentscript = file.Name.ToString();
readscript = File.ReadAllText(file.FullName).ToLower();
//Goes through each "Field" value from the column and sets to Lower Case//
foreach (DataRow dr in origdata.Tables["Fields"].Rows)
{
searchtext = dr["ToRemove"].ToString().ToLower();
//If the Field value appears in the file it's currently looking at, it'll put it
into our new dataset's new datatable//
if (readscript.Contains(searchtext))
{
DataRow row = newdata.Tables["Scripts"].NewRow();
row["ToRemove"] = searchtext;
row["ScriptName"] = currentscript;
row["RelatedTable"] = dr["Table"];
newdata.Tables["Scripts"].Rows.Add(row);
}
}
//Whilst going through the files in the specified folder, we also look at what tables from origdata that are mentioned in the files as these are the defunct tables and need flagging//
foreach (DataRow dr in origdata.Tables["Tables"].Rows)
{
searchtext = dr["Tables"].ToString();
if (readscript.Contains(searchtext))
{
DataRow row = newdata.Tables["Tables"].NewRow();
row["TableName"] = searchtext;
row["ScriptName"] = currentscript;
newdata.Tables["Tables"].Rows.Add(row);
}
}
}
newdata.ToWorkBook(output);
Process.Start(output);
}
}
}
【问题讨论】:
-
嗨杰,你把你的断点放在哪里?因为如果您的断点位于 Run 方法中,那么执行 F11(Step Into) 可能不起作用,因为您正在另一个线程上调用 DefunctRun 方法。如果是这样,请尝试将断点放在 DefunctRun 方法上。希望这会有所帮助。
-
感谢您的评论。不幸的是,它似乎也不喜欢那样。我回到 MainWindow.xaml 然后在尝试单步执行时不会去其他任何地方。
-
你在 try 中没有 Catch 块,所以如果它出错,那么什么都不会发生,你也不知道出了什么问题。添加一个catch块,在里面打个break,看看有没有错误。
-
添加了一个 catch 块,但没有错误触发它运行
-
您的 DefunctFields 类似乎被定义为部分,这是有原因的吗?我似乎将它用作服务,在这种情况下无需将其定义为部分类。