c#


asp.net mvc async await making main thread not blocking


public async Task<ActionResult> Search(string q)
{
var data = db.MediaPlanBilingInvoices.Where(m => m.IsDeleted == false);
//allthe stuff
decimal? allinvoicetotal=0;
decimal? allrototal = 0;
if (Noofrecords > 0)
{
if (WebConstants.PrintMedia)
{
var roobject = data.Where(x => x.isPrint == true).SelectMany(m => m.MediaPlanBilingInvoiceDetails);
var roobjects =roobject.Select(m=>m.RoId);
Func<decimal?> functions = new Func<decimal?>(() => roobject.Sum(m => m.MediaPlanRO.MediaPlanROPrints.Sum(qq => qq.MediaPlanPrint.Amount)));
var tasksro = await System.Threading.Tasks.Task.Factory.StartNew<decimal?>(functions);
allrototal = allrototal + tasksro.Result;
}
else if (WebConstants.ElectronicMedia)
{
var roobject = data.Where(x => x.iselectronic == true).SelectMany(m => m.MediaPlanBilingInvoiceDetails);
var roobjects =roobject.Select(m=>m.RoId);
Func<decimal?> functions = new Func<decimal?>(() => roobject.Sum(m => m.MediaPlanRO.MediaPlanROPrints.Sum(qq => qq.MediaPlanPrint.Amount)));
var tasksro = await System.Threading.Tasks.Task.Factory.StartNew<decimal?>functions);
allrototal = allrototal + tasksro ;
}
}
}
return Viewdata);
}
}
I am trying to calculate the sum of the ros in the different table for different criteria.
The problem is that when the program reachers allrototal it stops and doesn't go to the next line until it bring down the result. I want to keep it running not stop there. how can i do it?
The core problem you're having is that async doesn't change the HTTP protocol (discussed in detail on my blog). With HTTP, you have one request and one response. If you return a response, then you can't also have something else running and then return another response later - HTTP just doesn't work that way.
So, on ASP.NET, async allows you to free up the thread but does not complete the response. The response is not sent until the async method is finished.
Furthermore, Task.Run is safer than StartNew, but neither of them should be used like this on ASP.NET because all it's doing is freeing up the request thread pool thread by using up another thread pool thread. So await Task.Run(() => functions()); is just a more expensive way of saying functions().
This method should just be made synchronous. There's no asynchronous work to do here. If you want your client to behave asynchronously, then your client will need to make its HTTP request asynchronously.

Related Links

Implementing a collection initializer for my List wrapper in C# [duplicate]
How to scan a directory with wildcard with a specific subdirectory
Display Position of an Item in a List
C# shared folder access gives Logon failure exception
How to set System.Drawing.Image to System.ui.WebControls.Image in datalist template item
Could not load file or assembly CrystalDecisions.VSDesigner Version=10.5.3700.0
How to add certificate rule through command line or programmatically?
Not reading the content of files in wpf [closed]
Binding Visibility Converter in WPF C#
Setting alias name for the gridview field
Force GC to use another thread-context
C# code to get maximum dpi of installed or selected printer
Eventhandler not removed with new database
how to create a custom list c#
Setup Entity Framework For Dynamic Connection String
How to see/debug System.Diagnostics Counter?

Categories

HOME
optimization
grpc
nagios
ebay-api
spotfire
magento-1.9.2.1
pandoc
procmon
jquery-select2
google-api-oauth
rtc
ionic-native
ethereum
jpql
ndepend
drush
watson-dialog
popup
samba
indri
loader
gpo
bootstrap-datetimepicker
gnu-screen
ckeditor4.x
jira-zephyr
mongoose-im
utc
azure-table-storage
flickr
restler
perf
area
qt-quick
apiman
text-extraction
scrapinghub
anonymous-function
nao-robot
denodo
multivalue
audio-fingerprinting
visualizer
multi-select
tsc
iban
msiexec
sharing
portaudio
android-music-player
mavlink
foreground
netlink
sqlxml
salesforce-service-cloud
trendline
nslocalizedstring
sdkman
vanilla-forums
jsoncpp
avrcp
euro
opennebula
valdr-bean-validation
veeam
optimistic-locking
dojox.grid
jsonschema2pojo
ccmenuitem
object-oriented-database
data-layers
flask-mongoengine
facebook-likebox
nsnumber
kademlia
removeall
generator-expression
orchestration
mail-gem
hypertable
certificate-revocation
sup
reflexil
scsf
jqzoom
umfpack
session-hijacking
peoplepicker
funambol
symfony-1.2
designated-initializer
lwp
oaw
newspeak
data-formats
script-tag
unauthorized
ironpython-studio
tracd

Resources

Encrypt Message