How to remove all event handlers in Windows App?

57 views Asked by At

Here is the situation:

    public async void Test()
    {
        List<string> fileTypeFilter = new List<string>();
        fileTypeFilter.Add(".jpg");
        fileTypeFilter.Add(".png");
        var folder = KnownFolders.PicturesLibrary;
        var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);
        var queryResults = folder.CreateFileQueryWithOptions(queryOptions);
        //queryResults.ContentsChanged += null;
        queryResults.ContentsChanged += QueryResults_ContentsChanged;
    }

I call Test many times, so when some changes happened in that folder, QueryResults_ContentsChanged fires for manytimes, but I just want only once. I tried "+= null", but it does not work, so I have no idea how to remove all event handlers from the local variable queryResults.

2

There are 2 answers

0
Hari Prasad On BEST ANSWER

I don't see any use case that requires calling your Test method many times, I suggest verify/validate your design.

What I understood from your question is, you want to check whether QueryResults_ContentsChanged is attached to any other event or not, it is not possible (unless you've your own logic).

In general, an event can be unsubscribed as below.

queryResults.ContentsChanged -= QueryResults_ContentsChanged;

Hope this helps.

0
Manish Parakhiya On

You can use -= operator to Unsubscribe event.

queryResults.ContentsChanged -= QueryResults_ContentsChanged;

See MSDN for more detail that how to Subscribe and Unsubscribe events