Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a progress bar and label to indicate the progress of execution. but progress value and label text is not printing as expected. In each of the method i assign the value and text to static property which can be accessed from the UI.
Problem is, initial text is not showed and its stuck with "Post Expense completed". Please help me to display the label message and progress bar sequentially.

Main Method which calling from UI
C#
public class MainMethod
    {
        public static void PostData(DataSet ds, PostBasicInput pbi)
        {
            Task t1 = Task.Factory.StartNew(() => TaskClass.PostBasicPlan(ds, pbi)).ContinueWith((prevtask) => TaskClass.PostMembers(ds, pbi));
            Task.WaitAny(t1);
            Task.Factory.StartNew(() => TaskClass.PostIncome(ds, pbi));
            Task.Factory.StartNew(() => TaskClass.PostExpense(ds, pbi));
        }
    }


C#
public class TaskClass
   {
       public static void PostBasicPlan(DataSet ds, PostBasicInput pbi)
       {
           TestSettings.progresstext = "Post basic started";
           TestSettings.progressvalue = 10;
           Thread.Sleep(2000);
           TestSettings.progresstext = "Post basic completed";
           TestSettings.progressvalue = 20;
       }
       public static void PostMembers(DataSet ds, PostBasicInput pbi)
       {
           TestSettings.progresstext = "Post Member  started";
           TestSettings.progressvalue = 30;
           Thread.Sleep(2000);
           TestSettings.progresstext = "Post Member completed";
           TestSettings.progressvalue = 40;
       }
       public static void PostIncome(DataSet ds, PostBasicInput pbi)
       {
           TestSettings.progresstext = "Post income started";
           TestSettings.progressvalue = 50;
           Thread.Sleep(2000);
           TestSettings.progresstext = "Post income completed";
           TestSettings.progressvalue = 60;
       }
       public static void PostExpense(DataSet ds, PostBasicInput pbi)
       {
           TestSettings.progresstext = "Post expense started";
           TestSettings.progressvalue = 70;
           Thread.Sleep(2000);
           TestSettings.progresstext = "Post expense completed";
           TestSettings.progressvalue = 80;
       }
       public static void PopulateExcel(String excelpath)
       {
           TestSettings.progresstext = "Excel file creation";
           Thread.Sleep(2000);
           TestSettings.progresstext = "Excel Generated sucessfully";
           TestSettings.progressvalue = 90;
       }
       public static void Results(String apipath)
       {
           TestSettings.progresstext = "Calling result API";
           Thread.Sleep(2000);
           TestSettings.progresstext = "Actual vs expected excel generated";
           TestSettings.progressvalue = 100;
       }
   }

Additional classes
C#
public class PostBasicInput
  {
      public string excelId { get; set; }
      public int planId { get; set; }
      public string timeverificationstamp { get; set; }
  }
  public class TestSettings
  {
      public static int progressvalue { get; set; }
      public static string progresstext { get; set; }
  }


What I have tried:

Button click event
C#
  private void button1_Click(object sender, EventArgs e)
        {          
            DataSet ds=new DataSet();
            PostBasicInput pbi=new PostBasicInput();
            timer1.Start();
            Task t1= Task.Factory.StartNew(()=> MainMethod.PostData(ds, pbi));
            Task t2 = Task.Factory.StartNew(() => TaskClass.PopulateExcel("xyz"));
            Task.WaitAll(t1, t2);
            TaskClass.Results("http://");
            lblmsg.Text = "Completed..";
        }
//timer tick event
        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value = TestSettings.progressvalue;
            lblmsg.Text = TestSettings.progresstext;
        }  
Posted
Updated 7-Aug-16 2:38am
v2
Comments
Kornfeld Eliyahu Peter 7-Aug-16 8:38am    
Do you try to update the UI from a different thread?
jinesh sam 7-Aug-16 8:45am    
yes its separate thread
Kornfeld Eliyahu Peter 7-Aug-16 8:50am    
That's your problem! There is some problems, with that called 'thread safety'...
There are lot of articles if you search Google right...
May you start here: https://msdn.microsoft.com/en-us/windows/uwp/debug-test-perf/keep-the-ui-thread-responsive
RickZeeland 7-Aug-16 10:23am    
I would make the class TestSettings static too.
Also I would use progressBar1.InvokeRequired in timer1_Tick().
See: https://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900