当应用程序处理另一个线程时,更新主窗体标签中的文本(Update text in the label of main form while application is processing an a

编程入门 行业动态 更新时间:2024-10-28 00:24:23
当应用程序处理另一个线程时,更新主窗体标签中的文本(Update text in the label of main form while application is processing an another thread)

我创建了一个线程来在我的应用程序中执行某些功能,并在执行它时我想更新用户可见的应用程序主窗体中的标签。

我试图通过我调用usinag seprate线程的函数返回字符串数据,但它不起作用。

如果有任何解决方案在使用线程执行活动时更新标签文本,请告诉我。

class e2ertaData : e2erta1 { public void rsData() { network networkDetails = new network(); csv csvFile = new csv(); ftpFile ftpData = new ftpFile(); //Host Geo Data string getIP = networkDetails.GetIP(); string[] hostData = getIP.Split('~'); GeoIP geoIPReq = new GeoIP(); GeoIpData geoIPReqData = new GeoIpData(); geoIPReqData = geoIPReq.GetMy(); if (geoIPReqData.KeyValue["Error"].ToString() == "NO") { //Reading server names from XML file XmlDocument thisXmlDoc = new XmlDocument(); thisXmlDoc.LoadXml(ftpData.getConfigFile("server.xml")); XmlNodeList xnList = thisXmlDoc.SelectNodes("/servers/server"); //updating label in e2erta1 this.l1.Text = "daaaaaaaaaaa"; this.l1.Visible = true; this.l1.Refresh(); foreach (XmlNode xn in xnList) { string rtNote = ""; string requestedServer = xn["sname"].InnerText; string rtGet = networkDetails.GetRT(requestedServer); if (rtGet.Contains("Exception")) { rtNote = rtGet; //MessageBox.Show(rtNote); } try { var row = new List<string> { rtGet, rtNote }; ftpData.addToCSVFile(row); } catch (Exception c) { MessageBox.Show(c.ToString()); } } } else { MessageBox.Show("Geo data : " + geoIPReqData.KeyValue["Error"].ToString()); } //return null; } }

谢谢,

函数naveed

I have created a thread to perform certain functionality in my application and while performing it I want to update the label in the main form of the application which is visible to user.

I tried to return the string data through the function which I am calling usinag seprate thread but it does not work.

Please let me know if there is any solution to update the label text while performing an activity using thread.

class e2ertaData : e2erta1 { public void rsData() { network networkDetails = new network(); csv csvFile = new csv(); ftpFile ftpData = new ftpFile(); //Host Geo Data string getIP = networkDetails.GetIP(); string[] hostData = getIP.Split('~'); GeoIP geoIPReq = new GeoIP(); GeoIpData geoIPReqData = new GeoIpData(); geoIPReqData = geoIPReq.GetMy(); if (geoIPReqData.KeyValue["Error"].ToString() == "NO") { //Reading server names from XML file XmlDocument thisXmlDoc = new XmlDocument(); thisXmlDoc.LoadXml(ftpData.getConfigFile("server.xml")); XmlNodeList xnList = thisXmlDoc.SelectNodes("/servers/server"); //updating label in e2erta1 this.l1.Text = "daaaaaaaaaaa"; this.l1.Visible = true; this.l1.Refresh(); foreach (XmlNode xn in xnList) { string rtNote = ""; string requestedServer = xn["sname"].InnerText; string rtGet = networkDetails.GetRT(requestedServer); if (rtGet.Contains("Exception")) { rtNote = rtGet; //MessageBox.Show(rtNote); } try { var row = new List<string> { rtGet, rtNote }; ftpData.addToCSVFile(row); } catch (Exception c) { MessageBox.Show(c.ToString()); } } } else { MessageBox.Show("Geo data : " + geoIPReqData.KeyValue["Error"].ToString()); } //return null; } }

Thanks,

Naveed

最满意答案

还要考虑使用BackgroundWorker组件。

将BackgroundWorker从ToolBox拖到表单中 将backgroundworker的属性WorkerReportsProgress设置为true 订阅backgroundworker的活动DoWork 订阅backgroundworker的活动ProgressChanged

内部DoWork事件处理程序运行您在线程中执行的所有操作,并调用ReportProgress方法将进度传递到表单:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // reading server names from XML file for (int i = 0; i < xnList.Count; i++) { XmlNode xn = xnList[i]; // process node // report percentage to UI thread int percentProgress = (i+1)*100/xnList.Count; backgroundWorker1.ReportProgress(percentProgress); } }

在ReportProgress事件处理程序内部,只需为label赋值:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { label1.Text = e.ProgressPercentage.ToString(); }

要启动后台处理,请调用backgroundWorker1.RunWorkerAsync();

更新:您的代码不起作用,因为控件只能从创建它们的线程(UI线程)更新。 因此,您应该使用Invoke在UI线程上执行更新功能。 您可以在此处找到示例和说明。

Also consider using BackgroundWorker component.

Drag BackgroundWorker from ToolBox to your form Set backgroundworker's property WorkerReportsProgress to true Subscribe to backgroundworker's event DoWork Subscribe to backgroundworker's event ProgressChanged

Inside DoWork event handler run all what you do in your thread, and call ReportProgress method to pass progress to your form:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // reading server names from XML file for (int i = 0; i < xnList.Count; i++) { XmlNode xn = xnList[i]; // process node // report percentage to UI thread int percentProgress = (i+1)*100/xnList.Count; backgroundWorker1.ReportProgress(percentProgress); } }

Inside ReportProgress event handler simply assign value to label:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { label1.Text = e.ProgressPercentage.ToString(); }

To start background processing call backgroundWorker1.RunWorkerAsync();

UPDATE: Your code is not working, because controls could be updated only from thread which created them (UI thread). So you should use Invoke to execute update functionality on UI thread. Example and description you can find here.

更多推荐

本文发布于:2023-08-06 19:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   线程   应用程序   文本   标签

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!