C
C#16mo 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]
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
----------------------------- (1)
----------------------------- (2)
c8c876ee-5961-4822-a9a5-ee086633c724_Text
----------------------------- (1)
salut salut
Note that
----------------------------- (2)
----------------------------- (2)
doesn't come out at the end if there's something in ClipboardData for some reason What am I doing wrong?
14 Replies
Anton
Anton16mo ago
what's 67108864?
TheRanger
TheRanger16mo ago
did u try debugging with the debugger? $debug
MODiX
MODiX16mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
TheRanger
TheRanger16mo ago
i assume u called this method twice
gush3l
gush3l16mo ago
around 64mb in text irc no, will try using it, thanks for the suggestion
Anton
Anton16mo ago
so what?
MODiX
MODiX16mo ago
TheRanger#3357
REPL Result: Success
(67108864 / 1024) / 1024
(67108864 / 1024) / 1024
Result: int
64
64
Compile: 310.866ms | Execution: 59.733ms | React with ❌ to remove this embed.
gush3l
gush3l16mo ago
i am ignoring texts that are bigger than 64mb atm i know that it could be like 256mb with the same lenght but 🥱
Anton
Anton16mo ago
Well then make that a constant that says ignoredTextLengthThreshold Also, why are you calling the GC there?
gush3l
gush3l16mo ago
because for big texts even if i don't use them, the ram gets used up for no reason
Anton
Anton16mo ago
also, it's 2x that, strings are UTF-16 chars are two bytes long Just so you know, running the gc does not remove your strings from the dictionary, and it's not even guaranteed to remove the text variable from memory And even if it would, don't call the gc manually It will run on its own when your program needs memory Idk, maybe you have a good reason to do what you did But if you did it just because, then don't do it
gush3l
gush3l16mo ago
ik they don't get removed from the dictionary and ik that gc is something that I shouldn't mess with, I will make a video of the ram usage so y understand why i did that probably and 100% there is a better way for the useless ram usage issue, but i will look into that more after i resolve my execution order thingy
Anton
Anton16mo ago
Running the GC is a very costly operation
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.