multithreadingtaskprogressiprogress

cs0123 - no overload for 'ProgressChanged' matches delegate 'EventHandler'<int> in .net core


I have trouble to register a progress changed event. with the above error message from the question title.

Code:

public class AudioFileSearcher
{
    public AudioFileSearcher(string searchPath, bool includeSubFolders, Views.SoundListView.SoundList parentView)
    {
        this.progress1 = new Progress<int>();
        progress1.ProgressChanged += backgroundWorker1_ProgressChanged; //<- Error Here!
    }
    // void backgroundWorker1_ProgressChanged(object sender, EventArgs e) // also not working
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Do something
    }
    public async Task FindAudioFiles(IProgress<int> progress)
    {
        foreach (string item in longItemList)
        {
            // do long operation
            if (progress != null)
            progress.Report(0);
        }
    }
}

Every single example and stack overflow question I checked states that the Issue lays in

void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

which would correctly be

void backgroundWorker1_ProgressChanged(object sender, EventArgs e) // also not working

other questions I checked about IProgress unfortunately all have incomplete or pseudo examples which do not work for me.


Solution

  • The examples I found were all faulty. rather than this:

    public class AudioFileSearcher
    {
        public AudioFileSearcher(string searchPath, bool includeSubFolders, Views.SoundListView.SoundList parentView)
        {
            this.progress1 = new Progress<int>();
            progress1.ProgressChanged += backgroundWorker1_ProgressChanged; //<- Error Here!
            Task.Run(async () => await FindAudioFiles(progress1));
        }
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Do something
        }
    }
    

    try this:

    public class AudioFileSearcher
    {
        public AudioFileSearcher(string searchPath, bool includeSubFolders, Views.SoundListView.SoundList parentView)
        {
            this.progress1 = new Progress<int>(
            {
                backgroundWorker1_ProgressChanged();
            });
            Task.Run(async () => await FindAudioFiles(progress1));
        }
        void backgroundWorker1_ProgressChanged()
        {
            // Do something
        }
    }