本文介绍了使代码线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是C#线程的新手,我想帮助使这段代码线程安全。 线程AGetData ; public void Get() { SetA_TimeSteps.Items。明确(); SetA_InformationTree.Nodes [ 2 ]。节点[ 1 ]。Nodes.Clear(); Capabilities.Load( http://datapoint.metoffice.gov.uk/public/data/layer ?/ wxobs /所有/ XML /功能键= 14963e76-bad2-49ca-8c12-f47afd52deee); for ( int x = 0 ; x < Capabilities.SelectNodes( /图层/图层[@ displayName ='降雨量'] /服务/时间/时间)。计数; x ++) {SetA_TimeSteps.Items.Add(Capabilities.SelectNodes( / Layers / Layer [@ displayName ='Rainfall'] / Service / Times / Time)[x] .InnerText); } SetA_LayerImage.LoadAsync(FUNCTIONS.CreateMapURL( obs, RADAR_UK_Composite_Highres, png,SetA_TimeSteps.Items [ 0 ]。ToString())); SetA_TimeSteps.SelectedIndex = 0 ; SetA_LastLive = SetA_TimeSteps.Items [ 0 ]。ToString(); SetA_LayerImage.Enabled = true ; SetA_TimeSteps.Enabled = true ; SetA_PrepareAnimation.Enabled = true ; SetA_ExportMapData.Enabled = true ; SetA_ExportDataOverlay.Enabled = true ; SetA_ExportDataInformation.Enabled = true ; foreach ( string item in SetA_TimeSteps.Items) {SetA_InformationTree.Nodes [ 2 ]。节点[ 1 ]。 Nodes.Add(item.ToString()); } SetA_InformationTree.ExpandAll(); } // --- GetData_Click --- private void SetA_GetData_Click( object sender,EventArgs e) // A --- { AGetData = new 线程(Get); AGetData.Start(); Throbber.ShowDialog(); } 应该发生的事情是这样的:当我点击GetData按钮(SetA_GetData_Click)时,应该单独启动线程(AGetData)。原始线程应显示带有GIF动画的加载表单,告诉用户它正在加载,并应在新线程完成工作时关闭。新线程应设置控件上的所有属性,并在线获取xml文件中的资源。 主窗体在原始线程上,包含所有我想从新线程设置的控件。我不想使用BackgroundWorker。主要问题是如何使代码线程安全以及如何在新线程完成时关闭加载表单。我也会说这个帖子可以重用。希望这是有道理的,谢谢。解决方案 I am new to threading in C# and I would like some help with making this code thread-safe.Thread AGetData; public void Get() { SetA_TimeSteps.Items.Clear(); SetA_InformationTree.Nodes[2].Nodes[1].Nodes.Clear(); Capabilities.Load("http://datapoint.metoffice.gov.uk/public/data/layer/wxobs/all/xml/capabilities?key=14963e76-bad2-49ca-8c12-f47afd52deee"); for (int x = 0; x < Capabilities.SelectNodes("/Layers/Layer[@displayName='Rainfall']/Service/Times/Time").Count; x++) { SetA_TimeSteps.Items.Add(Capabilities.SelectNodes("/Layers/Layer[@displayName='Rainfall']/Service/Times/Time")[x].InnerText); } SetA_LayerImage.LoadAsync(FUNCTIONS.CreateMapURL("obs", "RADAR_UK_Composite_Highres", "png", SetA_TimeSteps.Items[0].ToString())); SetA_TimeSteps.SelectedIndex = 0; SetA_LastLive = SetA_TimeSteps.Items[0].ToString(); SetA_LayerImage.Enabled = true; SetA_TimeSteps.Enabled = true; SetA_PrepareAnimation.Enabled = true; SetA_ExportMapData.Enabled = true; SetA_ExportDataOverlay.Enabled = true; SetA_ExportDataInformation.Enabled = true; foreach (string item in SetA_TimeSteps.Items) { SetA_InformationTree.Nodes[2].Nodes[1].Nodes.Add(item.ToString()); } SetA_InformationTree.ExpandAll(); } // ---GetData_Click--- private void SetA_GetData_Click(object sender, EventArgs e) //A--- { AGetData = new Thread(Get); AGetData.Start(); Throbber.ShowDialog(); }What should happen is like this: When I click the GetData button (SetA_GetData_Click), it should start a separate thread (AGetData). The original thread should show a loading form with an animated gif on to tell the user it is loading and should close when the new thread has finished doing the work. The new thread should set all of the properties on the controls and get the resources from the xml files online.The main form is on the original thread and contains all of the controls I want to set from a new thread. I don't want to use a BackgroundWorker. The main problem is how to make the code thread-safe and how to close the loading form when the new thread has finished. I also would lie this thread to be reusable. Hope this makes sense, thank you. 解决方案 这篇关于使代码线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 04:04