C
C#β€’2y ago
maria 🌟

βœ… how i can compute hash async

How i can compute sha1 hash async on framework 4
35 Replies
Angius
Angiusβ€’2y ago
It doesn't seem like something that'd need to be done asynchronously
maria 🌟
maria πŸŒŸβ€’2y ago
yes is for forms
Angius
Angiusβ€’2y ago
Even more of a reason to not do it asynchronously, then
maria 🌟
maria πŸŒŸβ€’2y ago
why then i get stuck on form app while hashing and set as no responding
Angius
Angiusβ€’2y ago
Ah, I see what you mean
maria 🌟
maria πŸŒŸβ€’2y ago
yes
maria 🌟
maria πŸŒŸβ€’2y ago
Angius
Angiusβ€’2y ago
And what's the error?
maria 🌟
maria πŸŒŸβ€’2y ago
System.Security.Cryptography on framework doenst have thath funcion :D
Angius
Angiusβ€’2y ago
So there's only a synchronous version of this method, then
maria 🌟
maria πŸŒŸβ€’2y ago
i could downooad the package thath uses net for thath
Angius
Angiusβ€’2y 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 πŸŒŸβ€’2y ago
and i can get the result?
Angius
Angiusβ€’2y ago
You should be able to, yes
MODiX
MODiXβ€’2y 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β€’2y ago
As demonstrated
maria 🌟
maria πŸŒŸβ€’2y ago
so byte[] UnpatchedFileHash = await Task.Run(() => DigestAlgorithm.ComputeHash(FileBufferStream)); interesting sometimes i am very dumb
Anton
Antonβ€’2y ago
It is synchronous tho
maria 🌟
maria πŸŒŸβ€’2y ago
amm what
Anton
Antonβ€’2y ago
what does computehash return?
jcotton42
jcotton42β€’2y 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β€’2y 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
Connor
Connorβ€’2y 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β€’2y 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β€’2y ago
ah yeah you're right that thought slipped my mind
Connor
Connorβ€’2y ago
I never realized Task.Run was fundamentally different to awaiting a Task.
sibber
sibberβ€’2y ago
why not, the thread pool automatically adjusts itll create a new thread
Connor
Connorβ€’2y ago
When you await a task normally, it’s ran on the UI thread, or whatever synchronization context you’re calling from
sibber
sibberβ€’2y ago
the continuation after the first await will be scheduled by the sync context
mrphil2105
mrphil2105β€’2y 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β€’2y ago
but it can create more threads and it probably will
canton7
canton7β€’2y 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β€’2y 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β€’2y ago
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
maria 🌟
maria πŸŒŸβ€’2y ago
a byte of the hash omg guys @Angius was right thath solved my problem thanks all
Want results from more Discord servers?
Add your server