C#C
C#3y ago
gush3l

❔ Why is the order of execution wrong?

I have this code inside an async clipboard changes listener
    public static Dictionary<string, object> ClipboardData = new();

    private async void TrackClipboardChanges_EventHandler(object sender, object e)
    {
        var dataPackageView = Clipboard.GetContent();
        if (dataPackageView.Contains(StandardDataFormats.Text))
        {
            string text;
            try
            {
                text = await dataPackageView.GetTextAsync();
                if (text.Length > 67108864)
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    return;
                }

                Debug.WriteLine("----------------------------- (1)");
                foreach (var dictionaryValue in ClipboardData.Values)
                {
                    Debug.WriteLine(dictionaryValue + " " + text);
                    if (dictionaryValue.ToString() == text) return;
                }
                Debug.WriteLine("----------------------------- (2)");

                AddEntryToClipboardData(ClipboardItemType.Text, text);
                return;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return;
            }
        }

        [More code but it doesn't matter in this case]


But the output in the debug console is the following

----------------------------- (1)
----------------------------- (2)
c8c876ee-5961-4822-a9a5-ee086633c724_Text
----------------------------- (1)
salut salut


Note that
----------------------------- (2)

doesn't come out at the end if there's something in ClipboardData for some reason

What am I doing wrong?
Was this page helpful?
❔ Why is the order of execution wrong? - C#