Dear Friends,
My requirement is simple, I need progress bar on my Web Application develop in Dot Net framework 2.0.
I have try using Update Panel but no use.
So i try according to my Logic, I have create one web form add one Server Side 'Button' Control, One Server Side 'DIV' control having Server Side 'Image' Control one gif Image which is work as Progress Bar image for my application and server side timer control.
Now i need two work in one single event, so i include Threading in my Server Side code.
When i click Button it start Thread which call my procedure and start my timer and same time i set my DIV control visible property to "true" once the procedure complete it return some output which i store in form level variable "_msg". after every time period my timer check my "_msg" variable if it fill with some data my timer stop thread and change DIV control visible property to false.
All work fine but my timer can not success to change my DIV visibility to false.
My Web Form as below
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Start Long Task!" />
<br />
<br />
<div id="ProgressBar" runat="server" visible="false">
<asp:Image ID="Image1" runat="server" Height="100px"
ImageUrl="~/media/loading.gif" Width="99px" />
</div>
</div>
</form>
</body>
</html>
My Server Side Code as below
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Threading;
using System.Text;
public partial class Default5 : System.Web.UI.Page
{
object _Msg;
Control MyControl = new Control();
Thread thread;
private System.Timers.Timer aTimer = new System.Timers.Timer(1000);
protected void Page_Load(object sender, EventArgs e)
{
}
private void LongTask()
{
using (SqlConnection _cn = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;User ID=sa;Password=abc@123"))
{
_cn.Open();
using (SqlCommand _cmd = new SqlCommand("WAITFOR DELAY '00:00:10'; select 'Hi'", _cn))
{
_Msg = _cmd.ExecuteScalar();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
aTimer.Enabled = true;
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
_Msg = "";
OpenProgressBar(this.Page);
thread = new Thread(new ThreadStart(LongTask));
thread.Start();
}
void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_Msg.ToString().Length > 0)
{
thread.Abort();
aTimer.Enabled = false;
aTimer.Dispose();
Control MyControl = Page.FindControl("ProgressBar");
MyControl.Visible = false;
}
}
public static void OpenProgressBar(System.Web.UI.Page Page)
{
Control MyControl = Page.FindControl("ProgressBar");
MyControl.Visible = true;
}
}