C# 为什么在将该方法用作ThreadStart()参数时需要重载该方法?
我想使用单独的线程保存文件。没有使用线程的经验C# 为什么在将该方法用作ThreadStart()参数时需要重载该方法?,c#,C#,我想使用单独的线程保存文件。没有使用线程的经验 private ObservableCollection<PersonEntitiy> allStaff; private Thread dataFileTransactionsThread; #region Constructor public staffRepository() { allStaff = getStaffDataFromTextFile(); dataFileTransactionsThread =
private ObservableCollection<PersonEntitiy> allStaff;
private Thread dataFileTransactionsThread;
#region Constructor
public staffRepository() {
allStaff = getStaffDataFromTextFile();
dataFileTransactionsThread = new Thread(new ThreadStart(UpdateDataFile));
}
#endregion
public void UpdateDataFile(ObservableCollection<PersonEntitiy> allStaff) {
dataFileTransactionsThread.Start();
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:"+ dataFileTransactionsThread.ThreadState);
string containsWillBeSaved = "";
// ...
File.WriteAllText(fullPathToDataFile, containsWillBeSaved);
System.Diagnostics.Debug.WriteLine("Data Save Successfull");
// depricated but did not deside what is better to use instead yet
dataFileTransactionsThread.Suspend();
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:" + dataFileTransactionsThread.ThreadState);
}
您需要重载它,因为
ThreadStart
的构造函数接受无参数void函数,并且您正试图使用一个接受参数的函数。您似乎也在从它的方法中启动线程,这不应该发生。我建议您尝试以下方法:
private ObservableCollection<PersonEntitiy> allStaff;
private Thread dataFileTransactionsThread;
#region Constructor
public staffRepository() {
allStaff = getStaffDataFromTextFile();
dataFileTransactionsThread = new Thread(UpdateDataFileThread);
}
#endregion
public void UpdateDataFile(ObservableCollection<PersonEntitiy> allStaff)
{
dataFileTransactionsThread.Start(allStaff);
// If you want to wait until the save finishes, uncomment the following line
// dataFileTransactionsThread.Join();
}
private void UpdateDataFileThread(object data) {
var allStaff = (ObservableCollection<PersonEntitiy>)data;
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:"+ dataFileTransactionsThread.ThreadState);
string containsWillBeSaved = "";
// ...
File.WriteAllText(fullPathToDataFile, containsWillBeSaved);
System.Diagnostics.Debug.WriteLine("Data Save Successfull");
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:" + dataFileTransactionsThread.ThreadState);
}
私人可观测集合全体员工;
私有线程DataFileTransactionThread;
#区域构造函数
公共员工资源库(){
allStaff=getStaffDataFromTextFile();
DataFileTransactionThread=新线程(UpdateDataFileThread);
}
#端区
公共作废更新数据文件(ObservableCollection allStaff)
{
DataFileTransactionThread.Start(allStaff);
//如果要等待保存完成,请取消对以下行的注释
//DataFileTransactionThread.Join();
}
私有void UpdateDataFileThread(对象数据){
var allStaff=(ObservableCollection)数据;
System.Diagnostics.Debug.WriteLine(“dataFileTransactions线程状态:“+DataFileTransactionThread.ThreadState”);
字符串containsWillBeSaved=“”;
// ...
writealText(fullPathToDataFile,containsWirleBeSaved);
System.Diagnostics.Debug.WriteLine(“数据保存成功”);
System.Diagnostics.Debug.WriteLine(“dataFileTransactions线程状态:“+DataFileTransactionThread.ThreadState”);
}
请注意,在必须重新构造
DataFileTransactionThread
对象之前,您只能调用UpdateDataFile
一次,如果您想避免这种情况,并且执意使用线程,您可以在publicUpdateDataFile
中构造thread对象,您需要重载它,因为ThreadStart
的构造函数采用无参数void函数,而您正试图使用一个采用参数的函数。您似乎也在从它的方法中启动线程,这不应该发生。我建议您尝试以下方法:
private ObservableCollection<PersonEntitiy> allStaff;
private Thread dataFileTransactionsThread;
#region Constructor
public staffRepository() {
allStaff = getStaffDataFromTextFile();
dataFileTransactionsThread = new Thread(UpdateDataFileThread);
}
#endregion
public void UpdateDataFile(ObservableCollection<PersonEntitiy> allStaff)
{
dataFileTransactionsThread.Start(allStaff);
// If you want to wait until the save finishes, uncomment the following line
// dataFileTransactionsThread.Join();
}
private void UpdateDataFileThread(object data) {
var allStaff = (ObservableCollection<PersonEntitiy>)data;
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:"+ dataFileTransactionsThread.ThreadState);
string containsWillBeSaved = "";
// ...
File.WriteAllText(fullPathToDataFile, containsWillBeSaved);
System.Diagnostics.Debug.WriteLine("Data Save Successfull");
System.Diagnostics.Debug.WriteLine("dataFileTransactions Thread Status:" + dataFileTransactionsThread.ThreadState);
}
私人可观测集合全体员工;
私有线程DataFileTransactionThread;
#区域构造函数
公共员工资源库(){
allStaff=getStaffDataFromTextFile();
DataFileTransactionThread=新线程(UpdateDataFileThread);
}
#端区
公共作废更新数据文件(ObservableCollection allStaff)
{
DataFileTransactionThread.Start(allStaff);
//如果要等待保存完成,请取消对以下行的注释
//DataFileTransactionThread.Join();
}
私有void UpdateDataFileThread(对象数据){
var allStaff=(ObservableCollection)数据;
System.Diagnostics.Debug.WriteLine(“dataFileTransactions线程状态:“+DataFileTransactionThread.ThreadState”);
字符串containsWillBeSaved=“”;
// ...
writealText(fullPathToDataFile,containsWirleBeSaved);
System.Diagnostics.Debug.WriteLine(“数据保存成功”);
System.Diagnostics.Debug.WriteLine(“dataFileTransactions线程状态:“+DataFileTransactionThread.ThreadState”);
}
请注意,在必须重新构造
DataFileTransactionThread
对象之前,您只能调用UpdateDataFile
一次,如果您想避免这种情况,并且执意使用线程,您可以在公共UpdateDataFile
中构造thread对象。你必须给它一个编译器可以接受的方法。如果您想要一个带参数的threadstart方法,为什么要将allStaff
设为一个字段,然后将其作为参数传递?@Ed Plunkett,谢谢您的回答。我需要理解以下链:支持我们从另一个类调用UpdateDataFile(ObservableCollection allStaff)
,而不是此方法启动DataFileTransactionThread
。下一步是什么?@GurebuBokofu我刚刚告诉过你:里面的代码会执行。如果没有代码,则什么也不会发生。这就是线程执行的方法。你给它的那个,只有你给它的那个,没有其他可能有相同名称的方法。你是说你想给它传递一个方法的引用,你想让它知道这个方法的名字是什么,然后出去寻找其他同名的方法并调用它们?它应该放在哪里?如果另外两个重载具有相同的名称怎么办?它应该叫哪一个?那太疯狂了。它只是执行你给它的东西。@Ed Plunkett,好的,明白了。谢谢你的解释。。你必须给它一个编译器可以接受的方法。如果您想要一个带参数的threadstart方法,为什么要将allStaff
设为一个字段,然后将其作为参数传递?@Ed Plunkett,谢谢您的回答。我需要理解以下链:支持我们从另一个类调用UpdateDataFile(ObservableCollection allStaff)
,而不是此方法启动DataFileTransactionThread
。下一步是什么?@GurebuBokofu我刚刚告诉过你:里面的代码会执行。如果没有代码,则什么也不会发生。这就是线程执行的方法。你给它的那个,只有你给它的那个,没有其他可能有相同名称的方法。你是说你想给它传递一个方法的引用,你想让它知道这个方法的名字是什么,然后出去寻找其他同名的方法并调用它们?它应该放在哪里?如果另外两个重载具有相同的名称怎么办?它应该叫哪一个?那太疯狂了。它只是执行你给它的东西。@Ed Plunkett,好的,明白了。感谢您的解释。在最后一行(System.Diagnostics.Debug.WriteLine(“dataFileTransactions线程状态:“+DataFileTransactionThread.ThreadState”);
)中,线程仍在运行。我需要在这里停止吗?一旦方法完成执行(在本例中是在该行之后),线程就会退出,此时dataFileTr