【发布时间】:2014-07-07 12:47:04
【问题描述】:
下面是compact framework 3.5启动线程的方法
public ScanEntry(string scanId)
{
InitializeComponent();
_scanId = scanId;
//reader = deviceFactory.Create();
//reader.YMEvent += new ScanEventHandler(reader_Reading);
//reader.Enable();
}
private void CasesEntry_Load(object sender, EventArgs e)
{
caseCounterLabel.Text = cases.Count.ToString();
scanIdValueLabel.Text = _scanId;
}
internal void menuItemNewScan_Click(object sender, EventArgs e)
{
System.Threading.ThreadStart threadDelegate = new System.Threading.ThreadStart(ScanEvents);
System.Threading.Thread newThread = new System.Threading.Thread(threadDelegate);
newThread.Start();
}
在线程上调用下面的方法
private void ScanEvents()
{
try
{
//some other codes
if (scanIdValueLabel.InvokeRequired)
{
scanIdValueLabel.Invoke((Action)(() => scanIdValueLabel.Text = "value"));
}
attributeNode = docEventFile.CreateNode(XmlNodeType.Element, "Attribute", string.Empty);
XMLUtils.CreateAttribute(docEventFile, attributeNode, "name", "SCANID");
XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);
attributeSetNode.AppendChild(attributeNode);
//some other codes
}
catch(Execption e)
{
Message.Show(e.Message);
}
}
错误:
TextAlign = 'scanIdValueLabel.TextAlign' threw an exception of type 'System.NotSupportedException'
base {System.SystemException} = {"Control.Invoke must be used to interact with controls created on a separate thread."}
排队
XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);
我在这一行收到Control.Invoke must be used to interact with controls created on a separate thread
XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);
我已经用谷歌搜索并尝试了该解决方案,但对我不起作用。任何人都可以帮助我这样做。
谢谢
【问题讨论】:
-
你不能从另一个线程访问
scanIdValueLabel.Text。 -
Message.Show 背后是什么,它是否与表单上的控件进行交互?
-
@S.Spieker 你能解释一下清楚的吗?
-
-
一般规则是,您应该并且不能从创建控件的线程之外的另一个线程访问表单属性...
标签: c# .net compact-framework