Tuesday, August 3, 2010

Asynchronous Programming Techniques

You will often have to create applications that perform time-consuming operations, such as fi le downloads or complex calculations. These operations can cause the user interface (UI) to lock up and become unresponsive, leading to an unpleasant user experience. By using asynchronous programming techniques, you can enable time-consuming operations to be run asynchronously, thus keeping the UI responsive while the operations are run.

You are frequently required to perform tasks that consume fairly large amounts of time, such as file downloads. The BackgroundWorker component provides an easy way to run timeconsuming processes in the background, thereby leaving the UI responsive and available for user input.

The BackgroundWorker component is designed to allow you to execute time-consuming
operations on a separate, dedicated thread. This allows you to run operations that take a lot of time, such as fi le downloads and database transactions, asynchronously and allow the UI to remain responsive.
The key method of the BackgroundWorker component is the RunWorkerAsync method.
When this method is called, the BackgroundWorker component raises the DoWork event. The code in the DoWork event handler is executed on a separate, dedicated thread, allowing the UI to remain responsive. Important members of the BackgroundWorker component are shown below:
1. CancellationPending Property - Indicates whether the application has requested cancellation of a background operation.
2. IsBusy Property - Indicates whether the BackgroundWorker is currently
running an asynchronous operation.
3. WorkerReportsProgress Property - Indicates whether the BackgroundWorker component can report progress updates.
4. WorkerSupportsCancellation Property - Indicates whether the BackgroundWorker component supports asynchronous cancellation.
5. CancelAsync Method - Requests cancellation of a pending background operation.
6. ReportProgress Method - Raises the ProgressChanged event.
7. RunWorkerAsync Method - Starts the execution of a background operation by
raising the DoWork event.
8. DoWork Event - Occurs when the RunWorkerAsync method is called. Code in the DoWork event handler is run on a separate and dedicated thread.
9. ProgressChanged Event - Occurs when ReportProgress is called.
10. RunWorkerCompleted Event - Occurs when the background operation has been completed or cancelled or has raised an exception.


Running a Background Process :
The RunWorkerAsync method of the BackgroundWorker component starts the execution of
the background process by raising the DoWork event. The code in the DoWork event handler
is executed on a separate thread. The following procedure explains how to create a background
process.
TO CREATE A BACKGROUND PROCESS WITH THE BACKGROUNDWORKER COMPONENT :
1. From the Toolbox, drag a BackgroundWorker component onto the form.




2. In the component tray, double-click the BackgroundWorker component to create the
default event handler for the DoWork event. Add the code that you want to run on the
separate thread. An example is shown below.
// C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Insert time-consuming operation here
}
3. Elsewhere in your code, start the time-consuming operation on a separate thread by
calling the RunWorkerAsync method, as shown:
// C#
backgroundWorker1.RunWorkerAsync();


Providing Parameters to the Background Process:
Sometimes you will want to run a background process that requires a parameter. For example,you might want to provide the address of a fi le for download. You can provide a parameter in the RunWorkerAsync method. This parameter will be available as the Argument property of the instance of DoWorkEventArgs in the DoWork event handler.

TO PROVIDE A PARAMETER TO A BACKGROUND PROCESS :
1. Include the parameter in the RunWorkerAsync call, as shown below:
// C#
backgroundWorker1.RunWorkerAsync("C:\\myfile.txt");
2. Retrieve the parameter from the DoWorkEventArgs.Argument property and cast it
appropriately to use it in the background process. An example is shown below:
// C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string myPath;
myPath = (string)e.Argument;
// Use the argument in the process
RunTimeConsumingProcess();
}

Announcing the Completion of a Background Process :
When the background process terminates, whether because the process is completed or
the process is cancelled, the RunWorkerCompleted event is raised. You can alert the user to the completion of a background process by handling the RunWorkerCompleted event. An example is shown below:


// C#
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
System. Windows.Forms.MessageBox.Show("Background process completed");
}

You can ascertain if the background process was cancelled by reading the e.Cancelled
property, as shown below:
// C#
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
System. Windows.Forms.MessageBox.Show ("Process was cancelled!");
}
else
{
System. Windows.Forms.MessageBox.Show("Process completed");
}
}

RETURNING A VALUE FROM A BACKGROUND PROCESS :
You might want to return a value from a background process. For example, if your process
is a complex calculation, you would want to return the end result. You can return a value by
setting the Result property of the DoWorkEventArgs in the DoWorkEventHandler. This value
will then be available in the RunWorkerCompleted event handler as the Result property of the
RunWorkerCompletedEventArgs parameter, as shown in the following example:
// C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Assigns the return value of a method named ComplexCalculation to
// e.Result
e.Result = ComplexCalculation();
}
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
System. Windows.Forms.MessageBox.Show("The result is " +
e.Result.ToString());
}

Cancelling a Background Process :
You might want to implement the ability to cancel a background process. Background-
Worker supports the ability to cancel a background process, but you must implement most of the cancellation code yourself. The WorkerSupportsCancellation property of the Background Worker component indicates whether the component supports cancellation. You can call the CancelAsync method to attempt to cancel the operation; doing so sets the CancellationPending property of the BackgroundWorker component to True. By polling the CancellationPending property of the BackgroundWorker component, you can determine whether or not to cancel the operation.

TO IMPLEMENT CANCELLATION FOR A BACKGROUND PROCESS :
1. In the Properties window, set the WorkerSupportsCancellation property to True to
enable the BackgroundWorker component to support cancellation.
2. Create a method that is called to cancel the background operation. The following
example demonstrates how to cancel a background operation in a Button.Click event
handler:
// C#
private void btnCancel_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
3. In the BackgroundWorker.DoWork event handler, poll the BackgroundWorker.CancellationPending
property and implement code to cancel the operation if it is True. You
should also set the e.Cancel property to True, as shown in the following example:
// C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <>

if (backgroundWorker1.CancellationPending)

{

e.Cancel = true; return;

}

}

}



Reporting Progress of a Background Process with BackgroundWorker:
For particularly time-consuming operations, you might want to report progress back to the primary thread. You can report progress of the background process by calling the Report- Progress method. This method raises the BackgroundWorker.ProgressChanged event and allows you to pass a parameter that indicates the percentage of progress that has been completed to the methods that handle that event. The following example demonstrates how to call the ReportProgress method from within the BackgroundWorker.DoWork event handler and then to update a ProgressBar control in the BackgroundWorker.ProgressChanged event handler:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1;i <>

{ RunTimeConsumingProcess();

// Calls the Report Progress method, indicating the percentage

// complete

backgroundWorker1.ReportProgress(i*10);

}

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

progressBar1.Value = e.ProgressPercentage;

}

Note that in order to report progress with the BackgroundWorker component you must set the WorkerReportsProgress property to True.

Thanks,

Paras Sanghani

How to Create Setup Project in vs 2008?

Setup Projects:
You can add a setup project to a solution to create a Windows Installer application for your solution. Setup projects are highly configurable and allow you to create directories on the target computer, copy files, modify the registry, and execute custom actions during installation. When compiled, a setup project produces an .msi file, which incorporates a setup wizard for the application. The .msi file can be distributed by disk, download, or file share. When it is clicked, the .msi file launches the application setup wizard and installs the application.

TO ADD A SETUP PROJECT TO YOUR SOLUTION:
1. From the File menu, choose Add and then New Project to open the Add New Project
dialog box.

2. In the Project Types pane, expand Other Project Types, and then click Setup And
Deployment.
3. In the Templates pane, click Setup Project, and then click OK.




Setup Project Editors :
Each setup project includes six editors that allow you to configure the contents and the
behavior of the setup project. These editors are:
􀁑 File System Editor:
Allows you to configure the installation of your application to the file system of the target computer.
􀁑 Registry Editor:
Allows you to write entries to the registry upon installation.
􀁑 File Types Editor:
Allows you to set associations between applications and file types.
􀁑 User Interface Editor:
Allows you to edit the user interface seen during installation for both regular installation and administrative installation.
􀁑 Custom Actions Editor:
Allows you to def ne custom actions to be performed during installation.
􀁑 Launch Conditions Editor:
Allows you to set conditions for launching the installation of your setup project.

You can open any of these editors by selecting the deployment project in Solution Explorer
and then selecting the appropriate editor from the View menu.



TO ADD OUTPUT FROM A PROJECT TO A DEPLOYMENT PROJECT :
1. Right-click Application Folder in the left-hand pane of the File System Editor, choose
Add, and then choose Project Output. The Add Project Output Group dialog box opens.
2. Choose the project outputs that you want to add to your setup project. All .exe and .dll
files created by the project are contained in Primary Output. You can also add other
project files to your setup project, such as localized resources, content files, or documentation
files, or, less frequently, debug symbols, source files, or Extensible Markup
Language (XML) serialization assemblies. Once you have selected the output to be
added to the folder, click OK.








TO CREATE A SHORTCUT AND ADD IT TO THE TARGET COMPUTER:

1. In the right-hand pane of the File System Editor, right-click the file for which you want
to create a shortcut and choose Create Shortcut. A shortcut to the file is created and
added to the pane.
2. Drag the shortcut from the right-hand pane to the appropriate folder in the left-hand
pane.

How to Select Prerequisties in Setup Project?

  • Right Click your project in your Solution Explorer
  • Select Properties
  • Select Prerequisties checkboxes what ever applicable
  • Press OK

Kindly find the following link containing the Screen cast for creating Setup Project:
http://www.screencast.com/t/MmZhYzI5