c#-5.0

How do I call an asynchronous process from an asp.net hosted module using C#5 async / await?

萝らか妹 提交于 2019-12-01 13:44:29
问题 I have a long running process that is called via a Nancy Module Get method. Rather than have the browser hang while the process runs I would like this call to be asynchronous, in other words I want the Get method to return right away and leave the long running process to do its thing. I could then check the status of the process at regular intervals and act accordingly. I am new to the async / await features of C#5 but I feel sure these are meant for just this kind of task. The code below

Shouldn't lesser number of threads be used if I use async?

丶灬走出姿态 提交于 2019-12-01 11:06:54
My understanding is if I use async, the thread makes the web request and moves on. When the response comes back another thread picks it up from there. So there are a lesser number of tied up threads sitting idle. Wouldn't this mean the maximum number of live threads would go down? But in the example below, the code that doesn't use async ends up using lesser number of threads. Can some one explain why? Code without async (uses lesser threads): using System; using System.Diagnostics; using System.IO; using System.Net; using System.Threading; namespace NoAsync { internal class Program { private

Shouldn't lesser number of threads be used if I use async?

核能气质少年 提交于 2019-12-01 09:40:27
问题 My understanding is if I use async, the thread makes the web request and moves on. When the response comes back another thread picks it up from there. So there are a lesser number of tied up threads sitting idle. Wouldn't this mean the maximum number of live threads would go down? But in the example below, the code that doesn't use async ends up using lesser number of threads. Can some one explain why? Code without async (uses lesser threads): using System; using System.Diagnostics; using

What are the major risks vs. benefits of using VS2010 Async CTP?

穿精又带淫゛_ 提交于 2019-12-01 07:28:23
问题 I'd like to use Visual Studio Async CTP (Version 3) for developing and testing in VS2010 SP1 on Windows XP SP3 mainly because my clients (as well as I) on Windows XP SP3. Ghere is a related discussion on MSDN forum: If I target .net 4.0 but run on a machine that has .net 4.5 will .net 4.0 WPF bugs still be there? Though, the NOTE ON "AS IS" LICENSE tells: While the Async CTP license does not prevent you using it at your own risk in production environments, we advise you not to. The goal of

EF6 alpha Async Await on an Entity Stored Procedure / Function Import?

一曲冷凌霜 提交于 2019-12-01 05:29:32
I'd like to apply the new async await functionality to Stored Procedures / Function Imports imported in my Entity model, but have as yet been unable to with the EF6 alpha. Is it yet possible in EF6 alpha2 (or the nightly build as of 20211) to call any of the new Async methods on an Entity Function Import (which calls a SQL Stored Procedure) that returns a collection of Complex Type? e.g. private async Task<IList<Company>> getInfo (string id) { using (CustomEntity context = new CustomEntity()) { var query = await context.customStoredProcedure(id).ToListAsync(); // ".ToListAsync()" method not

How to emulate C# 6 null-conditional in C# < 6

左心房为你撑大大i 提交于 2019-12-01 03:18:39
With C# 6.0 I can do this var isEqual = x.Id == y.Id && x.UpdatedAt == y.UpdatedAt && x.Name == y.Name && x.RulesUrl == y.RulesUrl && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Is there any nice solution to do this with C# < 6.0? I mean this part && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; Because in old projects we do not have possibility to use C# 6.0. How to write isEqual efficiently? x.OnGoingChallenges?.Count is equivalent to x.OnGoingChallenges != null ?

What can I do in C# 5 with .Net 4.5 that I couldn't do in C# 4 with .Net 4? [closed]

吃可爱长大的小学妹 提交于 2019-12-01 02:02:30
I have Visual Studio 2012 RC installed on Windows 8 Release Preview and my question is are there any useful new features not related to Metro, or is Metro what seperates .Net 4 and .Net 4.5? See What's New in the .NET Framework 4.5 and What's New for Visual C# in Visual Studio 2012 . The biggest new feature of C# 5.0 is better support for asynchronous operations, using the new await keyword. 来源: https://stackoverflow.com/questions/11548373/what-can-i-do-in-c-sharp-5-with-net-4-5-that-i-couldnt-do-in-c-sharp-4-with-n

Async and asynchronous methods clarification?

浪子不回头ぞ 提交于 2019-12-01 01:06:44
AFAIK - ( and I read a lot about it), asynchronous methods ( not asynchronous delegates !) exists to solve the "thread is blocked" problem when dealing with I/O operations like : reading a file or downloading a file : Richter shows it quite clearly here : Task<T> is not related to the i/o blocking issue. it is simply just like open a thread ( plus extra efficiency + functionality ) - but it still causes a thread to consume cpu quanta etc. And here is my question : I've read (msdn) that : An async method provides a convenient way to do potentially long-running work without blocking the caller's

Sending mail with task parallel library throws error

余生颓废 提交于 2019-12-01 00:45:56
CODE:- List<Task> tasks = new List<Task>(); foreach (var item in arr)//arr contains 1000+ mailids { tasks.Add(Task.Factory.StartNew(() => { using (MailMessage msg = new MailMessage()) { msg=getmsg();//p-shadow code no erorr here SmtpClient smtp = new SmtpClient(); smtp.Host = smtpServer; smtp.Port = smtpPort; smtp.EnableSsl = isSmtpSsl != 0 ? true : false; smtp.Credentials = new NetworkCredential(smtpUName,smtpPass); smtp.Timeout = int.MaxValue; smtp.Send(msg);//---throws error.... //sql code to insert db that mail is send } }, TaskCreationOptions.LongRunning)); } Task.WaitAll(tasks.ToArray

Reading from serial port asynchronously using Async await method

落爺英雄遲暮 提交于 2019-11-30 22:51:36
I'm using this way for reading from serial port : public static void Main() { SerialPort mySerialPort = new SerialPort("COM1"); mySerialPort.BaudRate = 9600; mySerialPort.Parity = Parity.None; mySerialPort.StopBits = StopBits.One; mySerialPort.DataBits = 8; mySerialPort.Handshake = Handshake.None; mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); mySerialPort.Open(); Console.WriteLine("Press any key to continue..."); Console.WriteLine(); Console.ReadKey(); mySerialPort.Close(); } private static void DataReceivedHandler(object sender,