2017-07-04 54 views
0

我有一個名爲frmEmployees的員工form,我需要加載幾個combo box以及諸如國家,類別,國籍等數據。在c#中完成組合框數據加載後加載Windows窗體

現在當用戶點擊打開frmEmployees時,窗口會卡住一點點然後打開。我認爲這是因爲數據加載和初始化combo box。 現在!我想要的是,點擊按鈕打開後frmEmployees運行一個進度條,直到數據加載完成,然後打開表單。

public frmEmployee() 
    { 
     InitializeComponent(); 
     con = new Connection(); 

     LoadComboboxDS(); 
    } 

我已經試過也

private void FrmEmployee_Load(object sender, EventArgs e) 
    { 
     LoadComboboxDS(); 
    } 



private void LoadComboboxDS() 
    { 
     //company 
     var _companies = con.Companies.Where(x => x.IsDeleted == false).ToList(); 
     _companies.Insert(0,new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() }); 
     cbCompany.DataSource = _companies; 
     cbCompany.DisplayMember = "Name"; 
     cbCompany.ValueMember = "ID"; 

     //gender 
     cbGender.DataSource = Enum.GetValues(typeof(Gender)); 

     //merital status 
     cbMeritalStatus.DataSource = Enum.GetValues(typeof(MaritalStatus)); 

     //citizenship 
     var _citizenships = con.Countries.Select(x => x.Citizenship).Distinct().ToList(); 
     _citizenships.Insert(0, "--Select--"); 
     cbCitizenship.DataSource = _citizenships; 
     cbCitizenship.DisplayMember = "Citizenship"; 

     //nationality 
     var _nations = con.Countries.Select(x => x.Name).Distinct().ToList(); 
     _nations.Insert(0, "--Select--"); 
     cbNationality.DataSource = _nations; 
     cbNationality.DisplayMember = "Name"; 

     //domicile 
     var _domiciles = con.Countries.Select(x => x.Name).Distinct().ToList(); 
     _domiciles.Insert(0, "--Select--"); 
     cbDomicile.DataSource = _domiciles; 
     cbDomicile.DisplayMember = "Name"; 

     //cast category 
     var _casts = con.CastCategories.Select(x => new {x.ShortText, x.Description}).Distinct().ToList(); 
     _casts.Insert(0, new { ShortText = "", Description = "--Select--" }); 
     cbCategory.DataSource = _casts; 
     cbCategory.DisplayMember = "Description"; 
     cbCategory.ValueMember = "ShortText"; 

     //religion 
     cbReligion.DataSource = Enum.GetValues(typeof(Religion)); 

    } 
+0

您應該使用進度的BackgroundWorker的bar.An更簡單的方法是簡單地將光標圖標更改爲加載符號。 – kurdy

+0

你能推薦任何示例代碼嗎?因爲我嘗試了一些,但沒有運氣 – Siraj

+0

@kurdy Backgroundworker不適用於UI元素。 UI控件需要在UI線程中加載。 – Harsh

回答

0

看看我做了什麼...如果任何人都可以查看我的代碼,這將是可觀的。

public class EmployeeFormDataRepesenter 
{ 
    public List<Company> Companies { get; set; } 
    public List<Country> Countries { get; set; } 
    public List<CastCategory> CastCategories { get; set; } 
} 

public void LoadData(EmployeeFormDataRepesenter representer) 
    { 

     //gender 
     cbGender.DataSource = Enum.GetValues(typeof(Gender)); 
     //merital status 
     cbMeritalStatus.DataSource = Enum.GetValues(typeof(MaritalStatus)); 
     //religion 
     cbReligion.DataSource = Enum.GetValues(typeof(Religion)); 

     //company 
     var _companies = representer.Companies; 
     _companies.Insert(0, new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() }); 
     cbCompany.DataSource = _companies; 
     cbCompany.DisplayMember = "Name"; 
     cbCompany.ValueMember = "ID"; 

     //citizenship 
     var _citizenships = representer.Countries.Select(x => x.Citizenship).ToList(); 
     _citizenships.Insert(0, "--Select--"); 
     cbCitizenship.DataSource = _citizenships; 
     cbCitizenship.DisplayMember = "Citizenship"; 

     //nationality 
     var _nations = representer.Countries.Select(x => x.Name).ToList(); 
     _nations.Insert(0, "--Select--"); 
     cbNationality.DataSource = _nations; 
     cbNationality.DisplayMember = "Name"; 

     //domicile 
     var _domiciles = representer.Countries.Select(x => x.Name).ToList(); 
     _domiciles.Insert(0, "--Select--"); 
     cbDomicile.DataSource = _domiciles; 
     cbDomicile.DisplayMember = "Name"; 

     //cast category 
     var _casts = representer.CastCategories.Select(x => new { x.ShortText, x.Description }).Distinct().ToList(); 
     _casts.Insert(0, new { ShortText = "", Description = "--Select--" }); 
     cbCategory.DataSource = _casts; 
     cbCategory.DisplayMember = "Description"; 
     cbCategory.ValueMember = "ShortText"; 
    } 




private async void btnEmplyee_Click(object sender, EventArgs e) 
    { 
     con = new Connection(); 
     Action showProgress =() => frmStatrup._progressBar.Visible = true; 
     Action hideProgress =() => frmStatrup._progressBar.Visible = false; 

     EmployeeFormDataRepesenter representer; 

     Task<List<Company>> _taskCompany = new Task<List<Company>>(() => 
     { 
      BeginInvoke(showProgress); 
      var list = con.Companies.ToListAsync(); 
      BeginInvoke(hideProgress); 
      if (list != null) 
       return list.Result; 
      return null; 
     }); 

     Task<List<Country>> _taskCountry = new Task<List<Country>>(() => 
     { 
      BeginInvoke(showProgress); 
      var list = con.Countries.ToListAsync(); 
      BeginInvoke(hideProgress); 
      if (list != null) 
       return list.Result; 
      return null; 
     }); 

     Task<List<CastCategory>> _taskCasts = new Task<List<CastCategory>>(() => 
     { 
      BeginInvoke(showProgress); 
      var list = con.CastCategories.ToListAsync(); 
      BeginInvoke(hideProgress); 
      if (list != null) 
       return list.Result; 
      return null; 
     }); 


     _taskCompany.Start(); 
     var _companies = await _taskCompany; 

     _taskCountry.Start(); 
     var _countries = await _taskCountry; 

     _taskCasts.Start(); 
     var _casts = await _taskCasts; 


     if (_companies.Count != 0) 
     { 
      representer = new EmployeeFormDataRepesenter(); 
      representer.Companies = _companies; 
      representer.Countries = _countries; 
      representer.CastCategories = _casts; 
     } 
     else 
     { 
      representer = null; 
     } 

     if (representer != null) 
     { 
      frmEmployee _emp = new frmEmployee(); 
      _emp.LoadData(representer); 
      Functions.OpenForm(_emp, this.ParentForm); 
     } 
    } 
0

您可以使用實體框架6的異步擴展方法使你的代碼異步的。

獨立的數據訪問和表示層,在第一:只有

// Wherever you are going to open frmEmployee 
public async Task openFrmEmployee_OnClick(object sender, EventArgs e) 
{ 
    var getCompaniesTask = MyRepository.GetCompanies(); 
    var getCitizenshipsTask = MyRepository.GetCitizenships(); 

    await Task.WhenAll(getCompaniesTask, getCitizenshipsTask); // UI thread is not blocked 

    var form = new FrmEmployee(getCompaniesTask.Result, getCitizenshipsTask.Result); // form is created with data 
} 

現在,您可以:

public static class MyRepository // consider not static, just an example 
{ 
    public static async Task<List<Company>> GetCompanies() 
    { 
     using (var connection = new Connection()) // consider factory 
     { 
      return await con.Companies.Where(x => x.IsDeleted == false).ToListAsync(); 
     } 
    } 

    public async Task<List<Citizenship>> GetCitizenships() 
    { 
     using (var connection = new Connection()) // factory? 
     { 
      return await con.Countries.Select(x => x.Citizenship).Distinct().ToList(); 
     } 
    } 
} 

然後,你可以同時運行所有這些任務並等待它們完成需要使表單接受構造函數中的完整數據,而不是使表單加載此數據中斷抽象:

public class FrmEmployees 
{ 
    public FrmEmployees(List<Company> companies, List<Citizenship> citizenships) 
    { 
     companies.Insert(0,new data.Models.CompanyModels.Company { Address = new data.Models.Address(), Code = null, Name = "--Select--", BaseCurrency = new data.Models.Currency() }); 
     cbCompany.DataSource = companies; 
     cbCompany.DisplayMember = "Name"; 
     cbCompany.ValueMember = "ID"; 

     citizenships.Insert(0, "--Select--"); 
     cbCitizenship.DataSource = _citizenships; 
     cbCitizenship.DisplayMember = "Citizenship"; 

     // etc. 
    } 
} 

一件重要的事情:您可以獲取許多任務,並將許多數據傳遞給窗體構造函數。如果所有這些數據經常被使用,那麼你可能想要將這些「獲得所有這些東西」邏輯封裝到一個地方,以消除可能的代碼重複。