Asynchronous Programming with C# 5.0

August 12th, 2013 | Posted by Vidya Vrat in .NET | C# - (0 Comments)

Because a synchronous method call can create a delay in program flow which turns out to be bad user experience. Hence, an asynchronous approach will be better. Asynchronous method call will return immediately so that the program can perform other operations while the called method completes its work in certain situations.

The asynchronous methods behavior is different than synchronous ones. You call them; they start executing, but return the control back to the thread which called them in no time; while they continue to execute on different thread.

In general, asynchronous programming makes sense in two cases as:
• If you are creating a UI intensive application in which user experience is prime concern. In this case, an asynchronous call allows the user interface to remain responsive. Unlike shown in Figure 1-1.

• If you have other complex or expensive computational work to do, you can continue; interacting with application UI while waiting for the response back from long running task.  Read Full Article Here…

Implementing an MDI Form

The Multiple-Document Interface (MDI) is a specification that defines a user interface for applications that enable the user to work with more than one document at the same time under one parent form (window).

Visualize the working style of an application in which you are allowed to open multiple forms in one parent container window, and all the open forms will get listed under the Windows menu.  Whereas having an individual window for each instance of the same application is termed as single document interface (SDI); applications such as Notepad, Microsoft Paint, Calculator, and so on, are SDI applications. SDI applications get opened only in their own windows and can become difficult to manage, unlike when you have multiple documents or forms open inside one MDI interface.

Hence, MDI applications follow a parent form and child form relationship model. MDI applications allow you to open, organize, and work with multiple documents at the same time by opening them under the context of the MDI parent form; therefore, once opened, they can’t be dragged out of it like an individual form.

The parent (MDI) form organizes and arranges all the child forms or documents that are currently open. You might have seen such options in many Windows applications under a Windows menu, such as Cascade, Tile Vertical, and so on.

Try It: Creating an MDI Parent Form with a Menu Bar

In this exercise, you will create an MDI form in the WinApp project. You will also see how to create a menu bar for the parent form, that will allow you to navigate to all the child forms. To do so, follow these steps:

  1. Navigate to Solution Explorer, select the WinApp project, right-click, and select “Add” -> “Windows form”. Change the Name value from “Form1.cs” to “ParentForm.cs”, and click “Add”.
  2. Select the newly added ParentForm in the Design View. Select the ParentForm form by clicking the form’s title bar, navigate to the Properties window, and set the following properties:

    • Set the “IsMdiContainer” property to True (the default value is False). Notice that the background color of the form has changed to dark gray.
    • Set the Size property’s Width to 546 and Height to 411.

     

  3. Drag a MenuStrip control to the ParentForm. In the top-left corner, you should now see a drop-down showing the text “Type Here”. Enter the text “Open Forms” in the drop-down. This will be your main, top-level menu.
  4. Now under the Open Forms menu, add a submenu by entering the text “Win App”.
  5. Under the Win App submenu, enter “User Info”.
  6. Now click the top menu, “Open Forms”, and on the right side of it, type “Help”. Under the Help menu, enter “Exit”.
  7. Now, click the top menu, on the right side of Help, type “Windows”.
  8. Under the Windows menu, add the following options as separate submenus: Cascade, Tile Horizontal, Tile Vertical, and Arrange Icons. These will help in arranging the child forms.
  9. Now it’s time to attach code to the submenus you have added under the main menu Open Forms. First, you’ll add code for the submenu Win App, that basically will open the WinApp form. In the Design View, double-click the “Win App” submenu, that will take you to the Code View. Under the click event, add the following code:

    WinApp objWA = new WinApp();
    objWA.Show();

  10. Now to associate functionality with the User Info submenu: double-click this submenu, and under the click event add the following code:

    UserInfo objUI = new UserInfo();
    objUI.Show();

  11. To associate functionality with the Exit submenu located under the Help main menu, double-click “Exit”, and under the click event add the following code:

    Application.Exit();

  12. Now you have the form-opening code functionality in place, and you are nearly set to run the application. But first, you need to set the ParentForm as the start-up object. To do so, open Program.cs, and modify the “Application.Run(new UserInfo());” statement to the following:

    Application.Run(new ParentForm());

  13. Now build the solution, and run the application by pressing F5; the MDI application will open and should look as in Figure 1-1.

    MDIForm1.jpg

    Figure 1-1. Running an MDI form application

  14. Now if you click “Win App” and then “User Info” then both the forms will open one by one. These forms can be opened and dragged outside of the MDI form. This is not an expected behavior from a MDI application, as shown in Figure 1-2.

    This issue will be addressed later in this chapter.

    MDIForm2.jpg

    Figure 1-2. Running an MDI form application

How It Works

Each Windows Forms form is a class and exposes a Show() function by an instance created for it. You use the following code, that is creating an object and then invoking the Show() method. This opens the other form from the MDI parent form.

This creates an instance of the WinApp form and opens it for you:

WinApp objWA = new WinApp();
objWA.Show();

The following code creates an instance of the UserInfo form and opens it for you:

UserInfo objUI = new UserInfo();

Read Full Article Here

.NET Code Access Security

August 6th, 2013 | Posted by Vidya Vrat in .NET | C# | CLR - (0 Comments)

Security is an essential part of an application and it should be taken into  consideration from the grass root level from an application’s design. Security  is all about protecting your assets from unauthorized actions.

But Code Access Security (CAS) is a feature of .NET CLR which enables you to  control the permissions which an individual .NET application have on your system  during its execution. As a developer, you must understand how to create  applications that work even when some permission is restricted or to restrict  your application to perform certain tasks for example, a user can’t  delete/create a file in C:\Windows directory Read Full Article Here