0

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();



        }

    }
}
Hmax
  • 314
  • 6
  • 16
  • https://stackoverflow.com/q/2242564/8898497 – Hmax May 29 '18 at 09:32
  • there are definitely better ways to do this then threads and timers in the onclick handler of a webform page/button control (with or without scriptmanager), but looking at your code - I see you are not waiting for timer to stop before finishing your request handling and response. – Brett Caswell May 29 '18 at 09:37
  • from my perspective on your code sample here, I think the next natural step for you to progress in web development, is to learn and implement `WebMethod` in your webforms project, and to wire up a client-side script to call on it. here is related (unanswered common data/payload issue) SO question: https://stackoverflow.com/questions/19110170/how-to-call-webmethod-in-asp-net-c-sharp – Brett Caswell May 29 '18 at 10:03
  • Thanks for your response. But, I am new to this Webmethods. However, I will try and let you know if I am succeeded. But it will be helpful if I can get a way to resolve the problem in above way. – polisetty dhana harsha May 29 '18 at 12:26

0 Answers0