Advance Threading and Tasks in C#

on
In my last article, Complete Threading Tutorial in C#, I explained basics of threading. I explained that how threads can be created, how locking is implemented to make your applications thread-safe and how exception handling is implemented in threaded applications. This article explains some more advanced concepts. However, if you are not familiar with threads, I would advise you to first read my aforementioned article and then come to this article for advanced topics. Without wasting any further time, let us jump straight to the point.Foreground and Background Threads Thread Signaling Thread Pool Tasks Creating a Task Long Running Tasks Returning Values from Tasks Exception Handling With Tasks Task Continuation Foreground and Background Threads Threads which are created explicitly by the user are called foreground threads. The running time of the application is driven by the foreground threads and as long as any of these threads are executing, application keeps running. Background... Read More →
Views: 170

Complete Threading Tutorial (C#)

on
Often times, applications need to perform multiple tasks at a time. I always share Microsoft’s word processor’s example. While you type in the word processor, the application lets you type and in the meantime it is running spell-check and dictionary in the background to inform you whenever you type wrong spelling of a word. Basically multiple tasks are being done in a single program. This is the principle concurrency, to take advantage of multiprocessor hardware and to run multiple application parts simultaneously. Concurrency results in a more responsive application. Concurrency in .NET is implemented via threads. For now, you just need to keep in mind that tasks that take too much time should be run in a separate thread so that application can perform other important tasks meanwhile. Let us start with the most fundamental concept of concurrency i.e Threading.How to create a thread? Join & Sleep Thread IO Bound Operations vs. CPU Bound Operations Locking and Thread Safety Passing... Read More →
Views: 195
.NET Framework comes with a set of debugging tools. Particularly if you are using and advanced IDE such as Visual Studio, you can use debugger and other diagnostic tools available in the IDE. This debugging, however, is only applicable during the development of the application. Once the application is developed and shipped and then if an error occurs, it is not easy to diagnose and remove those errors via IDE debuggers. To handle such runtime application errors, special information is added with the code which can be later on used to diagnose and treat the error and bugs once the application is deployed. This chapter throws light on those techniques. Let us start this article with conditional compilation.Conditional Compilation Debug & Trace What are TraceListeners? Code Contracts Conditional Compilation Conditional compilation, as the name suggests, refers to the compilation of certain area of code based on some condition. Conditional compilation is done via preprocessor... Read More →
Views: 247

Disposal & Garbage Collection in C#

on
Garbage collection and memory management is probably that domain of programming where .NET Framework beats most of the other application development platforms. Most of the time, garbage collection is automatically done by .NET runtime once the object gets out of scope. When a .NET object is not being referenced by any module in the application, the garbage collector automatically disposes it after some time. However, this is not the case with all the objects. There are some objects that do not release resources implicitly and you have to incorporate a piece of code in your application to make them release resources. These objects include, open files, operating system handles, locks and unmanaged code. In .NET Framework, the process of explicitly releasing memory and resources is called disposal. The managed code is implicitly released and is referred as garbage collection. The simplest difference in garbage collection and disposal is that the former is done implicitly most of the... Read More →
Views: 265
XML documents are a standards described by World Wide Web Consortium (W3C) that are used for transferring information over the internet. The .NET Framework provides several namespaces and classes that allow developers to read, write and interact with the XML documents. In my article on LINQ to XML, I showed you that how types contained by System.Xml.Linq namespace can be used to interact with XML documents. However, there are many other types that can be used to read, write and communicate with the XML documents. In this article, we are going to see some of these types. XmlReader XmlWriter XmlDocument XmlReader XmlReader class belongs to System.Xml namespace and is used for reading XML stream in a forward-only manner. XmlReader is mostly used to read low-level streams. Without wasting any further time on theory, I am going to explain you that actually XmlReader reads from a file and from a string. Have a look at our first example for this tutorial. Example1 ... Read More →
Views: 372

LINQ to XML

on
XML stands for Extensible Markup language that is most commonly used for transferring information over the World Wide Web. XML documents are simple to write as well as understand both for humans as well as for software applications. If you come across an XML document, with little attention, you can point out that what this document is all about. However, this is not enough. You can have an XML document that contains thousands of lines of information. In such cases it is not advantageous to read and process the XML document manually as it might be time consuming as well as tedious. The real power of XML comes with its ability to be read by software applications. Since, XML documents follow some standards and if somehow software applications are tweaked to understand those standards, large XML documents can be seamlessly processed as done by human. This is the idea behind LINQ to XML. Luckily for developers, .NET Framework provides built-in capability to interact with XML documents... Read More →
Views: 284
LINQ is Microsoft’s latest data access technology. LINQ stands for language integrated query and it can be leverage to execute complex queries in a simple and more understandable manner. LINQ provides diverse query execution functions with the help of special methods that are known as query operators. In this article, we are going to throw light on some of the most important and useful query operators that can be used to perform almost all the routine data access tasks. If you are totally new to LINQ, you might need to look at our introductory articles on LINQ, particularly, Getting Started with LINQ and Interpreted LINQ queries. However, if you have basic knowledge of LINQ queries, and entity framework, you can continue reading this article. On the basis of input and output, the LINQ standard query operators can be broadly classified into three categories as follows:Sequence to sequence. (Takes input as sequence & gives output as sequence). Sequence to element. (Takes input as... Read More →
Views: 282
Entity framework is Microsoft’s latest data access technology and is considered a replacement for ADO.NET. However, there are still plenty of features where ADO.NET beats entity framework, but developers are now switching to Entity Framework owing to its strong decoupling and separation of concerns between the business and data access logic. This article is not focused towards explaining ins and outs of entity framework; this article explains that how LINQ can integrate with entity framework to achieve the desired data access feature. We will first start with our article by explaining how LINQ to SQL and then we will move towards LINQ to Entity framework. If you are not much familiar with foundations of LINQ, I would advise you to first have a look at my other tutorials on Getting Started with LINQ and Interpreted LINQ queries. Basically both LINQ to Entity Framework and LINQ to SQL are object-relational mappers on which LINQ queries can be executed. The major difference between... Read More →
Views: 405

Subqueries & Interpreted Queries in LINQ

on
If you are beginner to LINQ, I will suggest you to first read Introduction to LINQ. However, if you have read that article or have some basic understanding of LINQ queries and lambda expressions, you can carry on with this article. In this article, I am going to explain you some more advanced LINQ concept. We will begin with sub-queries in LINQ and finally, we will discuss the concept of interpreted queries. While reading the tutorial, I will suggest you to test code sample yourself. I will also encourage you to modify them and see their behavior. Subqueries in LINQ Sub-query or commonly written as subquery is basically a query within a query. In order to write a subquery, you have to write a query after the lambda expression of the outer query. Subquery follows all the standard C# expression rules and in fact is a C# expression. Since, you can right any valid C# expression on the right side of a lambda expression, you can also write a subquery. The best way to learn this... Read More →
Views: 405
LINQ (Language Integrated Query) is arguable one of the most astonishing features of the .NET framework. If you have slightest idea of database programming, you must have written SQL queries. Something like “Select * Name From Customers”. This line was actually used to get names of all the customers from the customers table. This method of querying database is uniform and standard. However, .NET Framework’s powerful features such as type-safety, intelli-sense and code readability were not integrated by those simple SQL queries. In order to address this issue, LINQ was introduced in .NET Framework 3.5. LINQ allows you to query almost all collections that implement the IEnumerable<T> interface, be it any list, array, XML DOM, or a table in SQL Server database. Apart from several advantages of LINQ, static type checking and dynamic query composition are two of the major features. There are two namespaces that contain LINQ types: System.Linq and... Read More →
Views: 402