How to stop other threads in C#

Juan Dent 236 Reputation points
2022-08-02T21:18:03.487+00:00

I have a C# program with this line inside a loop:

Dispatcher.Invoke(updateProgressBar1, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, j });  

when I want to close the application by clicking the close button in the title bar, the application continues to run for a long time... how can I abort the work in the background?? How in general can I close all threads?

Thanks!
Juan

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

6 answers

Sort by: Most helpful
  1. Juan Dent 236 Reputation points
    2022-08-03T12:10:39.987+00:00
    private void ScanFolders(string path)  
            {  
                UpdateProgressBarDelegate updateProgressBar1 = new UpdateProgressBarDelegate(progressBar1.SetValue);  
                string[] folders;  
                    folders = Directory.GetDirectories(path);  
                var val = progressBar1.Value;  
      
                progressBar1.Minimum = 0;  
                progressBar1.Value = 0;  
      
                progressBar1.Maximum = folders.Count();  
                for (double j=0; j < folders.Count(); j++)  
                {  
                    textBlock1.Text = folders[(int)j];  
                    Dispatcher.Invoke(updateProgressBar1, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, j });  
                    ScanFolders(textBlock1.Text);  
                }  
                progressBar1.Value = progressBar1.Maximum;  
            }  
      
    

    This is the central function of my small app; is this enough?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.