【发布时间】:2013-09-08 18:11:48
【问题描述】:
您好,我是第一次在线程上工作,不确定我是否正确 我收到错误消息:
调用线程无法访问此对象,因为不同的线程拥有它”异常
private void ImportProductStatsButtonClick(object sender, EventArgs e)
{
// Get the currently selected manufacturer from the combo box
var selected = comboBoxCorporation.SelectedItem;
buttonProductStatsAndRetailerStats.Enabled = false;
buttonSummariseRetailerStats.Enabled = false;
buttonSummariseProductStats.Enabled = false;
// Do we have one?
if (selected != null)
{
// Extract the combo record
var corporation = (ComboBoxCorporrationItem)selected;
// Do we have one?
if (corporation.Corporation != null)
{
// yes
// Make this on a seperate thread so that the UI continues to work
var thread = new Thread(MigrateProductStats);
thread.Start(corporation.Corporation.Id); // This enables me to pick the manufacturer that we are summarizing for
}
}
}
private void MigrateProductStats(object corporationIdObj)
{
// after thread completion I need to Enable my buttons.
buttonProductStatsAndRetailerStats.Enabled = true;
buttonSummariseProductStats.Enabled = true;
}
【问题讨论】:
-
你必须使用 Control.Invoke 见msdn.microsoft.com/en-us/library/…
-
您是否尝试将您的例外输入到谷歌和搜索?
-
@I4V 我尝试了不同的方法无法找到解决方案。调查了bathinenivenkatesh.blogspot.co.uk/2011/07/…
-
@62071072 奇怪,因为第一次点击回答了你的问题:)
标签: c# multithreading winforms