Does parallel processing help to execute async-await code more faster in c#? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have multi-core laptop and I wanna make my application more faster on that machine, I think async-await and parallel programming help to achieve that feat.

Typically, Async and await is more about not blocking the current thread during a long-running operation.
Typically, parallel processing is used when the goal is to achieve an increase in performance.
That said, I think it's not uncommon to see both used together--you might have a desktop app that you want to ensure the UI thread is not blocked while you perform a long-running operation
so you could use Async and await to start the long-running operation on a non-UI thread (this way, your desktop app's GUI can still be responsive while the long-running operation is running on another thread
and you might also decide to introduce parallelism in the implementation of that long-running operation to have it complete faster

Related

Does Parallel.ForEach use threads from the ASP.NET thread pool? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've read that using Task.Run in an ASP.NET web app was a bad idea as it uses another thread from the thread pool and hence prevent this particular thread from being used to serve a request.
Isn't the same situation with Parallel.ForEach? Won't it use multiple threads from the thread pool and hence prevent these particular threads from being used to serve a request ?
Isn't the same problem with Parallel.ForEach? Won't it use multiple threads from the thread pool and hence prevent these particular threads from being used to serve a request?
Yes, it will. This means that you'd be decreasing the total throughput of your server by having requests using up multiple requests like this. That individual request would complete faster, but it would complete faster at the expense of other requests.
This also assumes that your machine is under a high load. If there isn't a sufficiently high load on the thread pool, then it could afford to dedicate more resources for that request without inhibiting the ability of other requests to be served, and dedicating more resources for that request may well speed up that request.

Thread State Management in Asynchronous Programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to asynchronous programming in .Net using C#. All I have understood so far is :
With asynchronous programming, threads that are waiting for a web service or database to return data are freed up to service new requests until the data the is received.
Once the data is received, the thread is restarted and continue processing the code that comes after that call.
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
If it uses a stack to handle that, Can some one please give me an insight into the process?
Thanks,
Mayank
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
Async method are divided into smaller chunks. Basically, when compiling async method, for every await keyword new method is generated.
Keep in mind, that this is a big simplification and it's all done behind the scenes and you really don't need to know how it works in order to use it.

Async/await is a fine syntactic sugar [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can we say that the C# async/await pattern is some kind of
(smart) sugar syntax ?
Because as far as I can see, at the very end, it "just" hides all the implementation of an (internal) worker thread for the programmer.
Yes, there is no CLR support for await. It goes away on compilation. Anything you can write with await you can write without it.
Awaiting tasks it is a fancy way of calling ContinueWith :)
Decompile an assembly that uses await to see how it works under the covers.
async/await is indeed syntactic sugar, however, it doesn't just hide threads. Just because you use async and await in a method does not mean that you are creating threads.
From the MSDN Article Asynchronous Programming with Async and Await (C# and Visual Basic):
The async and await keywords don't cause additional threads to be
created. Async methods don't require multithreading because an async
method doesn't run on its own thread. The method runs on the current
synchronization context and uses time on the thread only when the
method is active. You can use Task.Run to move CPU-bound work to a
background thread, but a background thread doesn't help with a process
that's just waiting for results to become available.

C# need 2 threads for long execution [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that loads data from a SharePoint site. It loads txt files, xml files, etc. Any of these "load" actions can take a lot of time because of the user's connectivity to the SharePoint. Therefore the whole windows form UI gets unresponsive until the data is loaded.
So I would like to know how can I easily create a thread for that "retrieval" of information while the whole windows forms UI still works and is operative.
You have a few options. I'm not going to provide exact code for any of them, but, I will provide you with research topics.
You can use a BackgroundWorker, Task.Run() or manage your own threading by doing Thread.Start(). Do you need to fire off an event when the downloading is finished? If so, you can do something like this:
var task = new Task(() => DoSomething());
task.ContinueWith(() => SignalDone(), TaskScheduler.FromCurrentSynchronizationContext());
task.Run();
The ContinueWith and TaskScheduler.FromCurrentSynchronizationContext will ensure that the signaling will be done on the UI thread to minimize race conditions. You're on your own if you're doing databinding to anything being populated.

Synchronous method vs Thread.Sleep() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't understand the difference between Thread.Sleep() and synchronous methods. Please explain..
Is synchronous method in C# is similar to Thread.Sleep()?.
No, other than Thread.Sleep is a synchronous method.
A synchronous method is one where the entirety of their functionality happens before returning control to the caller.
Thread.Sleep is a function which causes the executing thread to block for the specified number of milliseconds (not exactly, but close due to the CPU thread scheduler). It executes synchronously and wouldn't be as useful in an asynchronous method.
However, saying they are the same thing would be a misnomer, as there are plenty of synchronous methods that are not Thread.Sleep and do not use it either.

Categories

Resources