C
C#β€’17mo ago
maria 🌟

βœ… how i can compute hash async

How i can compute sha1 hash async on framework 4
35 Replies
Angius
Angiusβ€’17mo ago
It doesn't seem like something that'd need to be done asynchronously
maria 🌟
maria πŸŒŸβ€’17mo ago
yes is for forms
Angius
Angiusβ€’17mo ago
Even more of a reason to not do it asynchronously, then
maria 🌟
maria πŸŒŸβ€’17mo ago
why then i get stuck on form app while hashing and set as no responding
Angius
Angiusβ€’17mo ago
Ah, I see what you mean
maria 🌟
maria πŸŒŸβ€’17mo ago
yes
maria 🌟
maria πŸŒŸβ€’17mo ago
Angius
Angiusβ€’17mo ago
And what's the error?
maria 🌟
maria πŸŒŸβ€’17mo ago
System.Security.Cryptography on framework doenst have thath funcion :D
Angius
Angiusβ€’17mo ago
So there's only a synchronous version of this method, then
maria 🌟
maria πŸŒŸβ€’17mo ago
i could downooad the package thath uses net for thath
Angius
Angiusβ€’17mo ago
Idk, try Task.Run()? https://learn.microsoft.com/pl-pl/dotnet/api/system.threading.tasks.task.run?view=net-7.0 Just don't do what they did in the docs, the .Wait() thing await it properly
maria 🌟
maria πŸŒŸβ€’17mo ago
and i can get the result?
Angius
Angiusβ€’17mo ago
You should be able to, yes
MODiX
MODiXβ€’17mo ago
Angius#1586
REPL Result: Success
var x = await Task.Run(() => 2 + 2);
x
var x = await Task.Run(() => 2 + 2);
x
Result: int
4
4
Compile: 600.086ms | Execution: 85.131ms | React with ❌ to remove this embed.
Angius
Angiusβ€’17mo ago
As demonstrated
maria 🌟
maria πŸŒŸβ€’17mo ago
so byte[] UnpatchedFileHash = await Task.Run(() => DigestAlgorithm.ComputeHash(FileBufferStream)); interesting sometimes i am very dumb
Anton
Antonβ€’17mo ago
It is synchronous tho
maria 🌟
maria πŸŒŸβ€’17mo ago
amm what
Anton
Antonβ€’17mo ago
what does computehash return?
jcotton42
jcotton42β€’17mo ago
it is, but they're using Task.Run to offload the work from the UI thread so it doesn't block the UI
mrphil2105
mrphil2105β€’17mo ago
Don't use Task.Run if the hash function runs for more than like 100 ms It isn't designed for long-running operations
Playboi17
Playboi17β€’17mo ago
Putting synchronous code in an async fashion does not make it run asynchronously If DigestAlgorithm.ComputeHash is synchronous, running it as a task will be synchronous. That’s not how it works Tasks are ran on the main thread in a thread pool. While the synchronous code in an async function is running, execution on the main thread is blocked. Async only helps when there is actual async functions within a Task
jcotton42
jcotton42β€’17mo ago
await Task.Run(() => sync code here) waits asynchronously for the Task returned by Run to finish yes, the work being done is sync, not async, but the important thing is the calling thread (the UI thread in this case) is not blocked that's what I meant by "offload from the UI thread" I'm also not sure what you mean by "Tasks are ran on the main thread in a thread pool" the thread pool doesn't have a "main thread" afaik
Anton
Antonβ€’17mo ago
ah yeah you're right that thought slipped my mind
Playboi17
Playboi17β€’17mo ago
I never realized Task.Run was fundamentally different to awaiting a Task.
sibber
sibberβ€’17mo ago
why not, the thread pool automatically adjusts itll create a new thread
Playboi17
Playboi17β€’17mo ago
When you await a task normally, it’s ran on the UI thread, or whatever synchronization context you’re calling from
sibber
sibberβ€’17mo ago
the continuation after the first await will be scheduled by the sync context
mrphil2105
mrphil2105β€’17mo ago
No. Tasks don't run Not necessarily The thread pool can be starved of all threads if you perform too many long running operations
sibber
sibberβ€’17mo ago
but it can create more threads and it probably will
canton7
canton7β€’17mo ago
It responds pretty quickly. Especially in a desktop app, there won't be any problems sticking the odd long-running operation onto it, in practice Heck, I've abused it to the point of it needing to scale up to 10,000 threads before
jcotton42
jcotton42β€’17mo ago
define "too many" and we're talking a typical desktop app here it's probably not going to hit "too many" it's not Task.Run just queues the delegate it's given to the thread pool the return task completes when that work completes no true either I think you should read $nothread it's about async IO, but provides a good general overview
MODiX
MODiXβ€’17mo ago
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
maria 🌟
maria πŸŒŸβ€’17mo ago
a byte of the hash omg guys @Angius was right thath solved my problem thanks all