
AreaProg C# Tutorial - Serialize Objects to a File While storing information in memory is great, there comes a time your users will have to shut your application down. This means (probably) that you will need to write information to a file at some point, because you will want to store whatever data was in memory. Today, we are going to take a look at a feature built into .NET called Serialization that makes writing and reading data structures to and from a file extremely easy. For this example, let's say I want to create a program that keeps track of all the cars my friends own. I'm going to create two objects to achieve this: Car and Owner. //information about the carpublic class Car{ private string make; private string model; private int year; private Owner owner; public Car() { }} //information about the car's ownerpublic class Owner{ private string firstName; private string lastName; public Owner() { }} Since most of us have more than one friend, we're going to need to create a List of Car objects. List<Car> cars = new List<Car>();
Serialize List Tutorial A List can be serialized to the disk. We want to serialize (to a file) a List of objects. The next time the program runs, we get this List straight from the disk. List Example This is the first part of the code example. Program that describes serializable type: C# using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable()] public class Lizard { public string Type { get; set; } // String property for Lizard object public int Number { get; set; } // Number of lizards public bool Healthy { get; set; } // Whether lizard is healthy public Lizard(string t, int n, bool h) { Type = t; Number = n; Healthy = h; } } We see a class called Lizard, and it has three automatic properties. The Serializable attribute is specified right before the class definition. Example 2 The second part of this tutorial is the Main method in your C# console program. When you press "s", a new List of Lizard objects is created. File.Open Examples
.NET Framework 4 The .NET Framework is a development platform for building apps for Windows, Windows Phone, Windows Server, and Microsoft Azure. It consists of the common language runtime (CLR) and the .NET Framework class library, which includes classes, interfaces, and value types that support an extensive range of technologies. The .NET Framework provides a managed execution environment, simplified development and deployment, and integration with a variety of programming languages, including Visual Basic and Visual C#. The documentation for the .NET Framework includes an extensive class library reference, conceptual overviews, step-by-step procedures, and information about samples, compilers, and command-line tools. To locate the information that interests you, see the following list of main topic areas. Provides a comprehensive overview of the .NET Framework and links to additional resources. Describes key new features and changes in the latest versions of the .NET Framework.
C# Tutorial - Using The ThreadPool A thread pool takes away all the need to manage your threads - all you have to do is essentially say "hey! someone should go do this work!", and a thread in the process' thread pool will pick up the task and go execute it. And that is all there is to it. In fact, working with the ThreadPool is so easy, I'm going to throw all the code at you at once. We have three arrays at the top of the program: one for input to the work items (inputArray), one for the results (resultArray), and one for the ManualResetEvents (resetEvents). So we initialize these arrays, and then we get to a for loop, which is where we will be pushing out these work items. So what are we queuing here? So what are we doing in this DoWork function? Back up in main thread land, after it has pushed all these work items onto the ThreadPool queue, we hit the very important call WaitHandle.WaitAll(resetEvents). Pretty simple, eh? That's it for this intro to thread pools in C#.
.NET et les threads Le multithreading est utilisé un peu partout. Vous voulez faire un application réseau et vous avez besoin de traiter les données de façon parrallèle, ou bien vous voulez parralléliser des traitements de calcul, mettre à jour une interface graphique pendant qu'un traitement se déroule... Lorsque vous faites du multithreading, des "problèmes" apparaissent comme le partage de ressources ou l'interbloquage. Vous trouverez dans ce document comment manipuler les threads et comment gérer les problèmes liés au multithreading. Afin de pouvoir travailler avec les threads il faut utiliser l'espace de nom "System.Threading". Utilisation de l'espace de nom System.Threading Sélectionnez using System.Threading; Pour un premier exemple nous allons créer un thread qui rajoute 1 à une variable. Si vous lancer l'application plusieurs fois vous verrez que l'affichage varie. La méthode Suspend() permet de suspendre le thread. La méthode Resume() permet de relancer le thread. Synchronisation Utilisation du lock
Hosting EXE Applications in a WinForm project Download demo project and source- 15.6 Kb Introduction Though not a common task, recently I needed to take an existing executable application and embed it into an application I was building. Oddly enough, I did not need any interaction between my application and the existing EXE. As it ends up, this is not a difficult thing to do. To make it even easier, I created a custom C# control that allows you to specify the name of an executable you want embedded into your application. How does it work In design time, the user can specify the name of the executable to embed. Process p = null; try { p = System.Diagnostics.Process.Start(this.exeName); p.WaitForInputIdle(); appWin = p.MainWindowHandle; } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error"); } After launching, the code must then set the parent of the executable's main window to the control handle. Any time the control is resized, it must also resize the executable window. History
Preprocessor Directive Conditionally includes source code at compile-time. #IF nExpression1 | lExpression1 Commands [#ELIF nExpression2 | #ELIF lExpression2 Commands... #ELIF nExpressionN | #ELIF lExpressionN Commands] [#ELSE Commands] #ENDIF Parameters #IF nExpression1| lExpression1Commands nExpression1 specifies the numeric expression that is evaluated. If the expression is nonzero, the commands immediately following #IF are included in the compiled code. lExpression1 specifies the logical expression that is evaluated. If the expression is True (.T.), the commands immediately following #IF are included in the compiled code. #ELIF nExpression2| #ELIF lExpression2Commands #ELIF nExpressionN| #ELIF lExpressionNCommands If nExpression1 is 0 or lExpression1 is False (.F.), the #ELIF directives are evaluated. If nExpression2 is 0 or lExpression2 is False (.F.), the commands following #ELIF are not included in the compiled code. #ELSE Commands Indicates the end of the #IF statement. #IF ... When the #IF ...
RegExp ASP.NET Cours accéléré Steven A. Smith S'applique à : Microsoft® .NET Framework Microsoft® ASP.NET API d'expression régulière Résumé : Les expressions régulières constituent un outil extrêmement puissant pour manipuler du texte. Téléchargez le code source de cet article. Sommaire PrésentationBref historique des expressions régulièresExpressions simplesQuantifieursMétacaractèresClasses de caractèresJeu de métacaractères prédéfinisExemples d'expressionsValidation dans ASP.NETAPI d'expression régulièreOutils gratuitsRubriques avancéesConclusionRessourcesÀ propos de l'auteur Introduction Dans Microsoft®.NET Framework, le support des expressions régulières est excellent, et même dans Microsoft® ASP.NET il existe des contrôles qui dépendent du langage des expressions régulières. Cet article s'adresse aux débutants n'ayant aucune connaissance ou ayant une connaissance limitée des expressions régulières, mais qui maîtrisent ASP.NET et la programmation dans .NET. . Bref historique des expressions régulières .
Tray Icon There are many cases when it's advantageous to place an application's icon in the System Tray. For example, firewall/antivirus and instant messaging applications do this so as to run in the background and still be accessible to the user without crowding up the task bar. In this week's installment of my .NET Tips and Techniques series, I'll show you the steps involved in specifying that an application is to be minimized to the Tray, how to allow the user to restore the application by double-clicking the icon and how to create and respond to a System Tray icon's context menu. To get started, open an existing C# Windows form (or create a new one).Open the Visual Studio Toolbox.Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. Figure 1