background preloader


Facebook Twitter

C# - Asynchronously wait for Task<T> to complete with timeout. ExecutionContext vs SynchronizationContext - .NET Parallel Programming. I’ve been asked a few times recently various questions about ExecutionContext and SynchronizationContext, for example what the differences are between them, what it means to “flow” them, and how they relate to the new async/await keywords in C# and Visual Basic.

ExecutionContext vs SynchronizationContext - .NET Parallel Programming

I thought I’d try to tackle some of those questions here. PDB Files: What Every Developer Must Know. Thanks for visiting one of the most popular pieces I’ve ever written!

PDB Files: What Every Developer Must Know

Based on questions I’ve written a few follow up articles you might find useful to extend your PDB knowledge after reading this one. Keeping Specific PDB Files from Loading in the Debugger Do PDB Files Affect Performance? Correctly Creating Native C++ Release Build PDBs How Many Secrets do .NET PDB Files Really Contain? If you’d love to know more about debugging, check out my Mastering .NET Debugging or Mastering Windows Debugging (for native C++) courses over at our great on-demand learning site WintellectNOW.

Because you’re obviously a great developer for reading this, use the code JOHNR-2013 and get your first two weeks free! If you have any questions about PDB files ask a question in the comments, email me (john AT wintellect DOT com) or ask me on Twitter @JohnWintellect. Using Dump Files. Dump files with or without heaps; create a dump file; open a dump file; find the binaries, pdbs, and source file for a dump file.

Using Dump Files

A dump file is a snapshot of an app at the point in time the dump is taken. It shows what process was executing and what modules were loaded. If the dump was saved with heap information, the dump file contains a snapshot of what was in the app's memory at that point in time. Opening a dump file with a heap in Visual Studio is like stopping at a breakpoint in a debug session. Although you cannot continue execution, you can examine the stacks, threads, and variable values of the app at the time the dump occurred. Debugging dump files created on another machine possibly using a different version of the .net framework - Wallace Turner. Disclaimer - None of this article describes anything new or ground breaking, indeed Visual Studio 2010 has been out for quite some time.

Debugging dump files created on another machine possibly using a different version of the .net framework - Wallace Turner

Developers don't usually spend their whole day debugging dumps until such time that there is a problem - thus this article serves as a quick refresher in times of need. Intro. Don't Block in Asynchronous Code. One of my most famous blog posts is Don’t Block on Asynchronous Code, which took an in-depth look at how a synchronous method could deadlock if it blocked on asynchronous code (e.g., using Task.Wait or Task.Result).

Don't Block in Asynchronous Code

This is a fairly common beginner’s mistake. Don't Block on Async Code. This is a problem that is brought up repeatedly on the forums and Stack Overflow.

Don't Block on Async Code

I think it’s the most-asked question by async newcomers once they’ve learned the basics. UI Example Consider the example below. A button click will initiate a REST call and display the results in a text box (this sample is for Windows Forms, but the same principles apply to any UI application). // My "library" method.public static async Task<JObject> GetJsonAsync(Uri uri){ using (var client = new HttpClient()) { var jsonString = await client.GetStringAsync(uri); return JObject.Parse(jsonString); }} // My "top-level" method.public void Button1_Click(...){ var jsonTask = GetJsonAsync(...); textBox1.Text = jsonTask.Result;} The “GetJson” helper method takes care of making the actual REST call and parsing it as JSON.

MSDN Magazine: Parallel Computing - It's All About the SynchronizationContext. Multithreaded programming can be quite difficult, and there’s a tremendous body of concepts and tools to learn when one embarks on this task.

MSDN Magazine: Parallel Computing - It's All About the SynchronizationContext

To help out, the Microsoft .NET Framework provides the SynchronizationContext class. Unfortunately, many developers aren’t even aware of this useful tool. Regardless of the platform—whether it’s ASP.NET, Windows Forms, Windows Presentation Foundation (WPF), Silverlight or others—all .NET programs include the concept of SynchronizationContext, and all multithreading programmers can benefit from understanding and applying it. The Need for SynchronizationContext. There Is No Thread. This is an essential truth of async in its purest form: There is no thread.

There Is No Thread

The objectors to this truth are legion. “No,” they cry, “if I am awaiting an operation, there must be a thread that is doing the wait! Task.Run Etiquette Examples: Don't Use Task.Run in the Implementation. Task Schedulers. .NET Framework 4.6 and 4.5 The following example shows some tasks that are scheduled on the global queue and other tasks that are scheduled on the local queue.

Task Schedulers

Threads vs. Tasks – SLaks.Blog. .Net has three low-level mechanisms to run code in parallel: Thread, ThreadPool, and Task.

Threads vs. Tasks – SLaks.Blog

These three mechanism serve different purposes. Thread represents an actual OS-level thread, with its own stack and kernel resources. (technically, a CLR implementation could use fibers instead, but no existing CLR does this) Thread allows the highest degree of control; you can Abort() or Suspend() or Resume() a thread (though this is a very bad idea), you can observe its state, and you can set thread-level properties like the stack size, apartment state, or culture.

The problem with Thread is that OS threads are costly. Each thread you have consumes a non-trivial amount of memory for its stack, and adds additional CPU overhead as the processor context-switch between threads. Behind the .NET 4.5 Async Scene: The performance impact of Asynchronous programming in C# - Dynatrace APM Blog. Task Parallel Library: 1 of n. Introduction I recall the first time I created a UI in .NET that had to go and get some data from a database, and the amount of data that I fetched was way larger in production that my code assumed it would be in my dumbed down test setup. Guess what happened... my UI froze as soon as it used real data. The reason for this is that the UI thread (i.e., the only thread in my naive UI) was being used to carry out this fetching of data from the backend database. Yikes! Async/Await - Best Practices in Asynchronous Programming. These days there’s a wealth of information about the new async and await support in the Microsoft .NET Framework 4.5.

This article is intended as a “second step” in learning asynchronous programming; I assume that you’ve read at least one introductory article about it. This article presents nothing new, as the same advice can be found online in sources such as Stack Overflow, MSDN forums and the async/await FAQ. This article just highlights a few best practices that can get lost in the avalanche of available documentation. The best practices in this article are more what you’d call “guidelines” than actual rules. Asynchronous Programming with Async and Await (C# and Visual Basic) You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. However, traditional techniques for writing asynchronous applications can be complicated, making them difficult to write, debug, and maintain. Visual Studio 2012 introduces a simplified approach, async programming, that leverages asynchronous support in the .NET Framework 4.5 and the Windows Runtime.

The compiler does the difficult work that the developer used to do, and your application retains a logical structure that resembles synchronous code. As a result, you get all the advantages of asynchronous programming with a fraction of the effort. This topic provides an overview of when and how to use async programming and includes links to support topics that contain details and examples.

Asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web.