Show a Loading gif image till the process loads


In this article, I will explain how to show a loading gif image till a process loads in a desktop application . It will give AJAX like look to your windows application. Secondarily, This article also explains how to get real time stock quote in your desktop application from yahoo finance without making your application hang till it receives the response from the website.

Objectives :


A. Display a loading gif for a process taking time to execute (similar to the functioning of AJAX)
B. Getting real time stock quote in a desktop application

(It is going to be a 2 in 1 application)

Introduction :


Sometimes few processes in the project takes time to load or complete the operation and it hangs the application for few seconds. It gives a bad impression to the end user.

It happened with me when I was working on a "Personal Portfolio Management Solution" for managing share market transactions. In this application the requirement was to read the Last Trade Price(LTP) for stock from Internet. I got code for communication with the yahoo finance web and reading current stock values from there.

But the problem was - when the application sends request to yahoo finance for stock value, it used to get hanged till it receives reply. And the scenario become worst if the net connection is slow.

And I have seen on most of the websites people asking for a solution of showing a loading gif till a process completes the work.

For this problem I researched and finally got the solution.

(The reason behind mentioning the problem is to give an understanding to the fellow Spiders that in which scenarios this code will be very much useful)

I hope that it will give a new look to the applications developed by my fellow spiders.

Getting Started :


To Learn the concept let's make a Demo Application which will give you the current stock value of Reliance Communication shares(RCOM.NS).

I am taking my real life example because I think it will be best to learn this concept.

I am giving the StockEngine class. It has a static method Execute() to which we have to pass Stock Symbol (e.g. RCOM.NS for Reliance Communication).
The method communicates with yahoo finance, processes the response and returns the LTP of the stock.

Click Here to Download StockEngine class

Steps for Making the Demo Application :



1. Take a new Visual C# Project and Add the StockEngine class into it.

In your form

using MTRACKER;


2. On the form take a label lblLTP and and picture box pbLoad. Set Image in the pbLoad to Loading.gif and SizeMode to AutoSize.

3. Clear the lable and place pbLoad over the label. Make the visible property of pbLoad false.

4. Take a button on your form and name it as btnGet

5. Take a backgroundWorker control and name it as bgWroker.

6. Now on button's click



private void btnGet_Click(object sender, EventArgs e)
{
lblLTP.Text = string.Empty;
pbLoad.Visible = true;
btnGet.Enabled = false;
bgWorker.RunWorkerAsync("RCOM.NS");
}




7. On the DoWork event of bgworker



private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{

String Code = e.Argument as String;
StockEngine Rate = StockEngine.Execute(Code);
e.Result = Rate;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}



8. On the RunWorkerCompleted event of bgWroker



private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
StockEngine LatestPrice = e.Result as StockEngine;
lblLTP.Text = LatestPrice.Value.ToString();
btnGet.Enabled = true;
pbLoad.Visible = false;
}



9. Now make sure that you are connected with the Internet and run the application. Enjoy the new looks.

I am also attaching the same application. You can download it in case you are struggling with the code.
Password : sibtain


I hope that you will like this code and it will add value to your knowledge.

(Don't forget to rate the content and leave your responses.)

Thanks 'n Regards,
Sibtain Masih

"Man is still the most extraordinary computer of all."


Attachments

  • Demo Application (42930-7354-AjxLookForStockQuote-DNS-Resource.rar)
  • Comments

    Guest Author: Prodigy16 Dec 2012

    Create Solution, i m also searching for this.
    Thanks



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: