I am trying to get the total file count inside a folder, where lots of folders with millions of files need to be counted. For this, I tried using 'Directory.EnumerateFiles'. I am using an aspx page, where I will give the path in a textbox and clicks the button. I need to get the count in the label. Since I wanted to get the count every 5 sec, I am using Threading and Timer for 'Timer_Tick' event. But I could see it is not working.
After deployment in IIS, once I click the button, the page refreshes and shows the count as 0 in label, after that nothing happens. Can anyone suggest how to get the continuous count in this way.
I need the count like, At 0 sec: 0 At 2 sec : 150 At 4 sec : 350 etc..
Here is my code:
public partial class GetCount : System.Web.UI.Page
{
//public static long filecount;
protected void Page_Load(object sender, EventArgs e)
{
}
public class Constants
{
public static string ConnectionString;
public static string DestinationFilePath;
private static IList<Thread> _threads = new List<Thread>();
public static IList<Thread> Threads
{
get { return Constants._threads; }
set { Constants._threads = value; }
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
try {
Timer1.Enabled = true;
Session["filecount"]=0;
Thread th = new Thread(GetFileCount);
Session["ThreadName"] = System.Guid.NewGuid().ToString();
Constants.Threads.Add(th);
th.Name = Session["ThreadName"].ToString();
th.Start();
}
catch(Exception ex)
{
}
}
public void StopCurrentProcess()
{
try
{
foreach (Thread thread in Constants.Threads)
{
if (thread.Name == Session["ThreadName"].ToString())
{
Constants.Threads.Remove(thread);
thread.Abort();
}
}
}
catch (Exception ex)
{
}
}
public void GetFileCount()
{
string path = ConfigurationManager.AppSettings["Path"].ToString() + TextBox1.Text.ToString();
Session["filecount"] = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).Count();
Label1.Text = Session["filecount"].ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(1000);
Label1.Text = Session["filecount"].ToString();
}
}
}