叫我正在開發中C#的MDI應用與.NET 4.0。 每個MDI子項將是一個帶有標籤的窗體,其中包含帶有DataGridView
的組框。 我實現了一個用於管理線程的類。UI沒有更新時TableAdapter.Fill從另一個線程
這是我ThreadManager
類
public string StartNewThread(ThreadStart threadMethod, string threadName)
{
try
{
Thread thread = new Thread(() => threadMethod());
thread.Name = threadName + " (" + _threadCount++.ToString("D4") + ")";
thread.Start();
_threadList.Add(thread.Name, thread);
return thread.Name;
}
catch (Exception ex)
{
//Log and manage exceptions
}
return null;
}
要創建我使用了一些嚮導組件從Oracle開發工具VS庫DataGridViews的StartNewThread
方法。所以,在創建DataSource和DataSet之後,我使用從DataSource樹中拖放&拖動表並自動創建DataGridViews。
這是實際的工作代碼,子表單後面自動創建。
public partial class ScuoleNauticheForm : Form
{
public ScuoleNauticheForm()
{
InitializeComponent();
}
private void ScuoleNauticheForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.PERSONALE' table. You can move, or remove it, as needed.
this.PersonaleTableAdapter.Fill(this.DEVRAC_NauticheDataSet.PERSONALE);
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.NATANTI' table. You can move, or remove it, as needed.
this.NatantiTableAdapter.Fill(this.DEVRAC_NauticheDataSet.NATANTI);
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.SCUOLE' table. You can move, or remove it, as needed.
this.ScuoleTableAdapter.Fill(this.DEVRAC_NauticheDataSet.SCUOLE);
}
}
我想現在要做的是管理所有的加載/查詢/插入/更新/刪除操作上分離線程。現在我試圖創建一個新的線程來加載數據。
這是我試過的。
public partial class ScuoleNauticheForm : Form
{
private readonly ThreadManager _threadManager;
public ScuoleNauticheForm()
{
InitializeComponent();
_threadManager = ThreadManager.GetInstance();
}
private void ScuoleNauticheForm_Load(object sender, EventArgs e)
{
_threadManager.StartNewThread(LoadData, "LoadData");
}
#region DataBind
private void LoadData()
{
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.PERSONALE' table. You can move, or remove it, as needed.
this.PersonaleTableAdapter.Fill(this.DEVRAC_NauticheDataSet.PERSONALE);
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.NATANTI' table. You can move, or remove it, as needed.
this.NatantiTableAdapter.Fill(this.DEVRAC_NauticheDataSet.NATANTI);
// TODO: This line of code loads data into the 'dEVRAC_NauticheDataSet.SCUOLE' table. You can move, or remove it, as needed.
this.ScuoleTableAdapter.Fill(this.DEVRAC_NauticheDataSet.SCUOLE);
}
#endregion
}
它僅半......有沒有錯誤或異常,但如果我加載數據的方式,使用不同的Thread
,該DataGridviews不更新,並打開時,我沒有看到任何數據表格,即使我移動或調整它的大小。否則,使用自動生成的代碼,DataGridViews被正確填充。 但是,由於嚮導還向窗體添加了一個導航欄來瀏覽記錄,所以我注意到它的工作原理,因爲它計算了正確的記錄數,我可以使用箭頭(第一,上一個,下一個,最後一個)來移動跨越記錄。
這是顯示我的表單的圖像。 查看顯示正確記錄總數(14)的導航欄,並允許我瀏覽它們。
我需要使用delegates
?如果是這樣,我認爲這將是一個爛攤子......我應該創建多少個delegates
以及這些方法?還是有另一種解決方案?
- 更新1 -
我知道UI線程自動.NET等程序員不需要用代碼來管理他們的管理。那麼,它是否應該與管理中內置的.NET UI線程同步?也許我的線程Form.Load()
啓動干擾由.NET管理的UI線程?
- UPDATE 2 -
我試圖落實faby提出的解決方案。我用Task
邏輯替換了我的Thread
邏輯。應用程序的行爲是相同的,因此與Thread
一起工作的所有內容現在也可以與Task
一起使用。 但問題依然存在。由於我在.NET 4.0而不是.NET 4.5,我無法使用異步並等待。所以我不知道用這種方法UI是否會正常工作。 任何其他建議對.NET 4.0有效?
看看我更新的答案,有一招! – faby 2014-12-03 16:02:25