mirror of
https://github.com/chylex/Code-Statistics.git
synced 2025-04-09 19:15:41 +02:00
Redo project load cancel to be safe and wait for the cancel to be acknowledged
This commit is contained in:
parent
db1f33144e
commit
55a605b868
CodeStatistics
@ -49,12 +49,18 @@ namespace CodeStatistics.Forms{
|
||||
}
|
||||
|
||||
private void btnCancel_Click(object sender, EventArgs e){
|
||||
if (project != null)project.Cancel();
|
||||
else if (search != null)search.Cancel();
|
||||
if (project != null)project.Cancel(OnCancel);
|
||||
else if (search != null)search.Cancel(OnCancel);
|
||||
else return;
|
||||
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
btnCancel.Enabled = false;
|
||||
}
|
||||
|
||||
private void OnCancel(){
|
||||
Invoke(new MethodInvoker(() => {
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,11 @@ namespace CodeStatistics.Handling{
|
||||
class Project{
|
||||
public delegate void ProgressEventHandler(int percentage, int processedEntries, int totalEntries);
|
||||
public delegate void FinishEventHandler(Variables variables);
|
||||
public delegate void CancelEventHandler();
|
||||
|
||||
public event ProgressEventHandler Progress;
|
||||
public event FinishEventHandler Finish;
|
||||
private event CancelEventHandler CancelFinish;
|
||||
|
||||
private readonly FileSearchData searchData;
|
||||
private readonly CancellationTokenSource cancelToken;
|
||||
@ -52,7 +54,10 @@ namespace CodeStatistics.Handling{
|
||||
int folderHandlerWeight = folderHandlers.Sum(handler => handler.Weight);
|
||||
|
||||
foreach(string folder in searchData.Folders){
|
||||
if (cancelToken.IsCancellationRequested)return;
|
||||
if (cancelToken.IsCancellationRequested){
|
||||
if (CancelFinish != null)CancelFinish();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(IFolderHandler folderHandler in folderHandlers){
|
||||
folderHandler.Process(folder,variables);
|
||||
@ -66,7 +71,10 @@ namespace CodeStatistics.Handling{
|
||||
|
||||
// Files
|
||||
foreach(File file in searchData.Files){
|
||||
if (cancelToken.IsCancellationRequested)return;
|
||||
if (cancelToken.IsCancellationRequested){
|
||||
if (CancelFinish != null)CancelFinish();
|
||||
return;
|
||||
}
|
||||
|
||||
IFileHandler handler = HandlerList.GetFileHandler(file);
|
||||
handler.Process(file,variables);
|
||||
@ -83,8 +91,9 @@ namespace CodeStatistics.Handling{
|
||||
},cancelToken.Token).Start();
|
||||
}
|
||||
|
||||
public void Cancel(){
|
||||
public void Cancel(CancelEventHandler onCancelFinish){
|
||||
cancelToken.Cancel(false);
|
||||
CancelFinish += onCancelFinish;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,9 +10,11 @@ namespace CodeStatistics.Input{
|
||||
class FileSearch{
|
||||
public delegate void RefreshEventHandler(int entriesFound);
|
||||
public delegate void FinishEventHandler(FileSearchData searchData);
|
||||
public delegate void CancelEventHandler();
|
||||
|
||||
public event RefreshEventHandler Refresh;
|
||||
public event FinishEventHandler Finish;
|
||||
private event CancelEventHandler CancelFinish;
|
||||
|
||||
private readonly string[] rootFiles;
|
||||
private readonly CancellationTokenSource cancelToken;
|
||||
@ -40,7 +42,10 @@ namespace CodeStatistics.Input{
|
||||
updateNotice();
|
||||
|
||||
foreach(string rootFile in rootFiles){
|
||||
if (cancelToken.IsCancellationRequested)return;
|
||||
if (cancelToken.IsCancellationRequested){
|
||||
if (CancelFinish != null)CancelFinish();
|
||||
return;
|
||||
}
|
||||
|
||||
bool isDirectory;
|
||||
|
||||
@ -52,7 +57,10 @@ namespace CodeStatistics.Input{
|
||||
|
||||
if (isDirectory){
|
||||
foreach(IOEntry entry in EnumerateEntriesSafe(rootFile)){
|
||||
if (cancelToken.IsCancellationRequested)return;
|
||||
if (cancelToken.IsCancellationRequested){
|
||||
if (CancelFinish != null)CancelFinish();
|
||||
return;
|
||||
}
|
||||
|
||||
searchData.Add(entry);
|
||||
++entryCount[0];
|
||||
@ -71,8 +79,9 @@ namespace CodeStatistics.Input{
|
||||
},cancelToken.Token).Start();
|
||||
}
|
||||
|
||||
public void Cancel(){
|
||||
public void Cancel(CancelEventHandler onCancelFinish){
|
||||
cancelToken.Cancel(false);
|
||||
CancelFinish += onCancelFinish;
|
||||
}
|
||||
|
||||
private static IEnumerable<IOEntry> EnumerateEntriesSafe(string path){
|
||||
|
Loading…
Reference in New Issue
Block a user