C
C#2y ago
justaeris

✅ Unsubscribing from all events

Okay, to make it short, I need to unsubscribe from an anonymous event and the way I thought would work would just to unsub from all by using .GetInvocationList() but it appears it just doesn't exist, can anyone give a tip on how to do it ?
19 Replies
justaeris
justaerisOP2y ago
@Windows10CE thought I'd ping you if you don't mind
Aaron
Aaron2y ago
what was ph in that small snippet you sent actually, could you just throw it all in $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
justaeris
justaerisOP2y ago
public static async Task Download(Action<HttpProgressEventArgs>? onDataReceived = null, CancellationToken cancellationToken = default)
{
const string url = "https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-windows-amd64.zip";
var path = Environment.CurrentDirectory;
var zipPath = path + "\\ngrok-stable-windows-amd64.zip";
var ngrokPath = path + "\\ngrok.exe";

if (!Directory.Exists(path)) Directory.CreateDirectory(path);

if (File.Exists(ngrokPath)) return;

var handler = new HttpClientHandler { AllowAutoRedirect = true };
var ph = new ProgressMessageHandler(handler);

ph.HttpReceiveProgress += delegate(object? _, HttpProgressEventArgs args) { onDataReceived?.Invoke(args); };

using var client = new HttpClient(ph);
await client.GetAsync(url, cancellationToken);
// Read the incoming stream using a zip stream to decompress and save the file at the same time

var stream = await client.GetStreamAsync(url, cancellationToken);
// Unsubscribe from all events by getting the event list and clearing it
ph.HttpReceiveProgress.GetInvocationList().ToList().ForEach(x => ph.HttpReceiveProgress -= (HttpProgressEventHandler)x);

using var zipStream = new ZipArchive(stream, ZipArchiveMode.Read);
var entry = zipStream.Entries[0];
entry.ExtractToFile(ngrokPath, true);
File.Delete(zipPath);
File.Create("ngrok.yml");
}
public static async Task Download(Action<HttpProgressEventArgs>? onDataReceived = null, CancellationToken cancellationToken = default)
{
const string url = "https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-windows-amd64.zip";
var path = Environment.CurrentDirectory;
var zipPath = path + "\\ngrok-stable-windows-amd64.zip";
var ngrokPath = path + "\\ngrok.exe";

if (!Directory.Exists(path)) Directory.CreateDirectory(path);

if (File.Exists(ngrokPath)) return;

var handler = new HttpClientHandler { AllowAutoRedirect = true };
var ph = new ProgressMessageHandler(handler);

ph.HttpReceiveProgress += delegate(object? _, HttpProgressEventArgs args) { onDataReceived?.Invoke(args); };

using var client = new HttpClient(ph);
await client.GetAsync(url, cancellationToken);
// Read the incoming stream using a zip stream to decompress and save the file at the same time

var stream = await client.GetStreamAsync(url, cancellationToken);
// Unsubscribe from all events by getting the event list and clearing it
ph.HttpReceiveProgress.GetInvocationList().ToList().ForEach(x => ph.HttpReceiveProgress -= (HttpProgressEventHandler)x);

using var zipStream = new ZipArchive(stream, ZipArchiveMode.Read);
var entry = zipStream.Entries[0];
entry.ExtractToFile(ngrokPath, true);
File.Delete(zipPath);
File.Create("ngrok.yml");
}
fits in here unless you really want it in $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Aaron
Aaron2y ago
ah, it's not your class then all you can do really is keep track of your subscriptions yourself, or use reflection why do you need to unsubscribe anyway?
justaeris
justaerisOP2y ago
because for some unknown reasons, it reapeats the progress two times I mean I kndi know why but not how to fix it
Aaron
Aaron2y ago
I don't think this will fix it
justaeris
justaerisOP2y ago
like it does 0 to 100 when it downloads the files and 0 to 100 again when it's reading the stream, copying or whatever operation on it
Aaron
Aaron2y ago
you could try just having a bool that says whether to handle the event or not and check that in your delegate
justaeris
justaerisOP2y ago
oh oh btw whats the difference between ph.HttpReceiveProgress += delegate(object? _, HttpProgressEventArgs args) { onDataReceived?.Invoke(args); }; and ph.HttpReceiveProgress += (sender, args) => onDataReceived?.Invoke(args);
Aaron
Aaron2y ago
the first is just older syntax I'd always do the second one
justaeris
justaerisOP2y ago
oh okay thanks for the tip I'll try the bool to see if it fixes it
justaeris
justaerisOP2y ago
ah
Aaron
Aaron2y ago
idk why that's given as a warning should work fine
justaeris
justaerisOP2y ago
and it does bar just stays full for a long period of time now gotta find some sketchy workaround Task.Run moment nah I think it's fine or wait hold my beer bro I'm so smart
...
var handle = true;
ph.HttpReceiveProgress += (_, args) =>
{
if (handle) onDataReceived?.Invoke(args);
};

using var client = new HttpClient(ph);
await client.GetAsync(url, cancellationToken);
handle = false;
onDataReceived?.Invoke(new HttpProgressEventArgs(-1, null, 0, 0));
...
...
var handle = true;
ph.HttpReceiveProgress += (_, args) =>
{
if (handle) onDataReceived?.Invoke(args);
};

using var client = new HttpClient(ph);
await client.GetAsync(url, cancellationToken);
handle = false;
onDataReceived?.Invoke(new HttpProgressEventArgs(-1, null, 0, 0));
...
...
Dispatcher.Invoke(() =>
{
if (args.ProgressPercentage == -1)
OverallProgressBar.IsIndeterminate = true;
else
{
OverallProgressBar.IsIndeterminate = false;
OverallProgressBar.Value = args.ProgressPercentage;
}
parent.TaskbarItemInfo.ProgressValue = args.ProgressPercentage / 100f;
});
...
...
Dispatcher.Invoke(() =>
{
if (args.ProgressPercentage == -1)
OverallProgressBar.IsIndeterminate = true;
else
{
OverallProgressBar.IsIndeterminate = false;
OverallProgressBar.Value = args.ProgressPercentage;
}
parent.TaskbarItemInfo.ProgressValue = args.ProgressPercentage / 100f;
});
...
kinda cheating but it works just fine thanks again for your help and time you can close the thread if you can once you saw this
Anton
Anton2y ago
unsubbing all = setting to null isn't that allowed with events? yeah ig probably not
Aaron
Aaron2y ago
not if you're not in the declaring class
Want results from more Discord servers?
Add your server