如何在c#winforms中从位于web中的服务器数据库中获取数据时使用进度条?

时间:2017-03-29 05:23:52

标签: c# winforms

登录过程后,我想用mdi加载form1。

在Form1的Pageload事件中,使用JSon从Web加载数据库表数据并保存到本地数据库。 Form1 pageload需要很多时间。 当我执行我的应用程序时,首先加载mdi表单,并在一段时间后加载form1。 我想解决这个问题。这是我的页面加载事件代码。 这里从Web获取数据并保存到本地数据库。 然后将数据设置为form1中的控件。

private void Form1_Load(object sender, EventArgs e)
{
  string strUrlService = "http://.............................";
  string strUrlBranch = "http://......................";
  string strUrlOrigin = "http://.....";
  string strUrlEmployee = "http://.......";

  .............................
  GetServiceType(strUrlService);
  GetBranch(strUrlBranch);
  GetOrigin(strUrlOrigin);
  GetEmployee(strUrlEmployee);
  .............................

  SetServiceType();
  SetBranch();
  SetOrigin();
  SetEmployee();
  ......................

}

GetServiceType方法定义。使用Json对象从Web获取值。并将此数据保存到本地数据库。     // ---------从Web数据库获取ServiceType -------------

private void GetServiceType(string strUrl)
{
  ServiceType obj = new ServiceType();
  HttpClient client = new HttpClient();
  client.BaseAddress = new Uri(strUrl);
  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  HttpResponseMessage response = client.GetAsync(strUrl).Result;
  if (response.IsSuccessStatusCode)
  {
    var JsonResult = response.Content.ReadAsStringAsync().Result;
    System.Web.Script.Serialization.JavaScriptSerializer tmp = new System.Web.Script.Serialization.JavaScriptSerializer();
    obj = (ServiceType)tmp.Deserialize(JsonResult, typeof(ServiceType));
  }
  if (obj != null)
  {
    int count = obj.products.Count;
    int[] TxtID = new int[count];
    ..............................
    for (int i = 0; i < count; i++)
    {
      TxtID[i] = Convert.ToInt32(obj.products[i].id.ToString());
      ...............................
    }
    FunSaveServiceType(TxtID, txtService, txtCode, intDays);
  }
}

在SetServiceType()方法中,从本地数据库检索数据(从Web获取并保存到本地数据库)。并且此数据设置为form1中的组合框。

//--------------set service type to Form1 combobox control-----------------
private void SetServiceType()
{
  DataTable dtService = new DataTable();
  dtService = GetServiceTypeFromDB();
  DataRow dr = dtService.NewRow();
  dr["ServiceType"] = "";
  dtService.Rows.InsertAt(dr, 0);
  cbServiceType.DataSource = dtService;
}

这些是在form1 pageload事件之前完成的过程。这些代码工作正常。

我尝试以mdi格式设置进度条和后台工作程序。但它运作不正常。

我是C Sharp的新手。请为我的问题提出解决方案。

1 个答案:

答案 0 :(得分:0)

如果您使用.Net 4.5,您将不再需要背景工作者。您可以使用新的TPL(任务并行库)。您首先需要的是一个可以以不同形式显示的进度条。您也可以使用mainform显示进度,但是应该使用Form1.Shown事件。如果您的表单已启动并正在运行并且您的gui组件已正确加载,则会触发此操作。

您可以在Shown-Event中实现进度逻辑,如下所示:

private async void Form1_Shown(object sender, EventArgs e)
{
   var progressHandler = new Progress<string>(value =>
   {
      //What to do if a Progress is reported?
      //For example set progressbar and labeltext
      progressBar.Step();
      label1.Text = value; //Value stores the string you send via Report (see at bottom method)
   });

   await InitializeServicesAsync(progressHandler as IProgress<string>);
}

private async Task InitializeServicesAsync(IProgress<string> progress)
{      
  await Task.Run(() =>
  {
    string strUrlService = "http://.............................";
    string strUrlBranch = "http://......................";
    string strUrlOrigin = "http://.....";
    string strUrlEmployee = "http://.......";

    GetServiceType(strUrlService);
    progress.Report("Getting Servicetypes done");

    GetBranch(strUrlBranch);
    progress.Report("Getting Branch done");

    GetOrigin(strUrlOrigin);
    progress.Report("Getting Origin done");

    GetEmployee(strUrlEmployee);
    progress.Report("Getting Employee done");

    SetServiceType();
    progress.Report("Setting ServiceType done");

    SetBranch();
    //Report further progress as often as you need

    SetOrigin();
    SetEmployee();
  });
}

我希望这对你有所帮助。否则,您可以查看this blogpost以了解有关它的更多信息。