叫我正在开发中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