c#-5.0

C# async/await strange behavior in console app

我们两清 提交于 2019-12-19 16:33:11
问题 I built some async/await demo console app and get strange result. Code: class Program { public static void BeginLongIO(Action act) { Console.WriteLine("In BeginLongIO start... {0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); act(); Console.WriteLine("In BeginLongIO end... \t{0} {1}", (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); } public static Int32 EndLongIO

Async wait block Main UI

这一生的挚爱 提交于 2019-12-19 09:49:20
问题 I am using the new async await features to upgrade from backgroundworker in C#. In the following code I am trying to replicate the execution of multiple tasks with ContinueWith method. Task t1 = new Task ( () => { Thread.Sleep(10000); // make the Task throw an exception MessageBox.Show("This is T1"); } ); Task t2 = t1.ContinueWith ( (predecessorTask) => { if (predecessorTask.Exception != null) { MessageBox.Show("Predecessor Exception within Continuation"); return; } Thread.Sleep(1000);

Async and asynchronous methods clarification?

女生的网名这么多〃 提交于 2019-12-19 04:23:49
问题 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

How to wait until all tasks are finished before running code

和自甴很熟 提交于 2019-12-19 03:54:07
问题 I am trying to write a multi threading search and then display all the results once the tasks have finished running but currently I don't understand how to process the results once all the tasks are complete My code is as follows: private async void DoSearchAsync() { var productResults = await SearchProductsAsync(CoreCache.AllProducts); var brochureResults = await SearchBrochuresAsync(CoreCache.AllBrochures); _searchResults.AddRange(productResults); _searchResults.AddRange(brochureResults);

Determining the caller inside a setter — or setting properties, silently

旧城冷巷雨未停 提交于 2019-12-18 12:26:39
问题 Given a standard view model implementation, when a property changes, is there any way to determine the originator of the change? In other words, in the following view model, I would like the "sender" argument of the "PropertyChanged" event to be the actual object that called the Prop1 setter: public class ViewModel : INotifyPropertyChanged { public double Prop1 { get { return _prop1; } set { if (_prop1 == value) return; _prop1 = value; // here, can I determine the sender? RaisePropertyChanged

Can I have my assembly reference any version of another assembly? [duplicate]

≡放荡痞女 提交于 2019-12-18 12:23:30
问题 This question already has answers here : Is it possible to replace a reference to a strongly-named assembly with a “weak” reference? (3 answers) Closed last year . I am developing a class library ( MyClassLibrary ). I depend on a third party class library ( ThirdPartyClassLibrary ). I need to use the same version of ThirdPartyClassLibrary as my users. e.g., if I set a static value in ThirdPartyClassLibrary the user needs to see that change. Users of my class may be depending on any one of 4

Parallel Blob Upload throwing 404 Bad Request intermittently

允我心安 提交于 2019-12-18 04:53:06
问题 I have a very simple service, public class AzureService : IAzureService { private readonly CloudBlobContainer _container; public AzureService(ISettings settings) { var storageAccount = CloudStorageAccount.Parse(settings.BlobConnectionString); var blobClient = storageAccount.CreateCloudBlobClient(); _container = blobClient.GetContainerReference(settings.BlobContainerName); } public Task UploadBlobAsync(string fileName, Stream stream) { var blob = _container.GetBlockBlobReference(fileName);

How to solve Windows Azure Diagnostic Runtime Error (Could not create WindowsAzure.Diagnostics, Version=xx, Culture=neutral, PublicKeyToken=xx

早过忘川 提交于 2019-12-18 04:04:29
问题 privateLibManager libManager; private LibManager Connect() { this.libManager=new LibManager();//here we are getting an error } Error: The type initializer for 'SWConfigDataClientLib.LibManager' threw an exception Inner Exception: Could not create Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics,Version=2.2.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35. Source IpPbxCDSClientLib 回答1: First make sure you have added references to

Async all the way down?

泄露秘密 提交于 2019-12-18 03:56:13
问题 Trying to understand the new async/await pattern, I have one question which I can't find an answer to, namely if I should decorate my methods with async, if I intend to call those methods from other async functions, or just return Task s where appropriate? In other words, which of these classes A, B or C is best, and why? class A<T> { public async Task<T> foo1() //Should be consumed { return await foo2(); } public async Task<T> foo2() //Could be consumed { return await foo3(); } private async

C# async methods still hang UI

本秂侑毒 提交于 2019-12-17 19:21:35
问题 I have these two methods, that I want to run async to keep the UI responsive. However, it's still hanging the UI. Any suggestions? async void DoScrape() { var feed = new Feed(); var results = await feed.GetList(); foreach (var itemObject in results) { var item = new ListViewItem(itemObject.Title); item.SubItems.Add(itemObject.Link); item.SubItems.Add(itemObject.Description); LstResults.Items.Add(item); } } public class Feed { async public Task<List<ItemObject>> GetList() { var client = new