积极答复者
如何判断这段程序执行完了,谢谢。

问题
-
下面是旅行商问题的一段代码,城市坐标的多少会影响程序执行完毕的时间,单步调试发现不管坐标数量多
少,退出public void DrawTour(object sender,TspEventArgs e){}方法,就是下面一个大括号,程
序就结束了,但退出大括号后程序指向哪里了呢?如何做能判断程序结束了呢?谢谢你的指点。
public void DrawTour(object sender, TspEventArgs e) { this.lastFitnessValue.Text = Math.Round(e.BestTour.Fitness, 2).ToString(CultureInfo.CurrentCulture); //合理的值 this.lastIterationValue.Text = e.Generation.ToString(CultureInfo.CurrentCulture); //循环值 if (cityImage == null) { cityImage = new Bitmap(tourDiagram.Width, tourDiagram.Height); cityGraphics = Graphics.FromImage(cityImage); } int lastCity = 0; int nextCity = e.BestTour[0].Connection1; cityGraphics.FillRectangle(Brushes.White, 0, 0, cityImage.Width, cityImage.Height); foreach( City city in e.CityList ) { // Draw a circle for the city. //城市用圆代表。 cityGraphics.DrawEllipse(Pens.Red, city.Location.X - 2, city.Location.Y - 2, 5, 5); // Draw the line connecting the city. //绘制线条连接城市。 cityGraphics.DrawLine(Pens.Black, cityList[lastCity].Location, cityList[nextCity].Location); //****************************************** ////表示添加文本 StreamWriter sw1 = File.AppendText("D:\\Tsp.txt"); string w1 =Convert.ToString(cityList[lastCity].Location+"\r\n"); sw1.Write(w1); sw1.Close(); //************************************ // figure out if the next city in the list is [0] or [1] //计算出,如果在列表中的下一个城市是[ 0 ]或[1] if (lastCity != e.BestTour[nextCity].Connection1) { lastCity = nextCity; nextCity = e.BestTour[nextCity].Connection1; } else { lastCity = nextCity; nextCity = e.BestTour[nextCity].Connection2; } } this.tourDiagram.Image = cityImage; //旅行图 if (e.Complete) { StartButton.Text = "Begin";//开始 StatusLabel.Text = "Open a City List or click the map to place cities."; //打开城市列表,或点击地图放置城市 StatusLabel.ForeColor = Color.Black; } }
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
答案
-
代码段中:黑色粗体部分分不清楚,或用其他颜色表示,谢谢。
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
那是我用一般的Button举一个例子。这是反射内部代码源码部分,你直接看不到的。
我的意思是: 如果你的方法是绑定到某个事件上的,那么应该先执行诸如触发事件的代码,以便触发事件,随后执行你的代码,最终完成整个事件的处理。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 杲大盛 2013年9月11日 2:40
全部回复
-
这个函数谁调用的?是一个事件吧。事件是由某个内部函数触发的,事件只是内部函数触发的处理结果。处理完毕之后结束本事件处理,接着回到这个内部函数的下面继续执行(如果该函数不是你自己定义,而是内部的,恐怕你看不到),需要Reflector调试得到。
比如说Button的Click事件处理完毕之后,继续处理Button类内部的代码(反射才可以看到):
protected override void OnClick(EventArgs e) { Form form = base.FindFormInternal(); if (form != null) { form.DialogResult = this.dialogResult; } base.AccessibilityNotifyClients(AccessibleEvents.StateChange, -1); base.AccessibilityNotifyClients(AccessibleEvents.NameChange, -1); base.OnClick(e); }
黑色粗体部分处理Click事件,处理完毕之后如果黑色粗体部分下面还有代码,继续进行;没有就到此结束了。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
你可以把DrawTour方法内的内容放在一个单独的方法中定义, 该方法定义为具有返回值的,如果方法完成就返回为true,
如果执行过程出现异常则返回false,然后再DrawTour方法中对该方法进行调用,如果返回true则表示方法执行完成,
如果返回为false,则表示方法执行过程中发生错误了。如下示例代码所示:
public void DrawTour(object sender, TspEventArgs e) { if(DrawTourComplete()) { // 执行完成 } else { // 执行过程中发生错误 } } public bool DrawTourComplete() { try { // DrawTour方法内的代码放在try语句中 } catch(Exception) { // 返回false表示执行过程中发生错误了 return false; } return true; }
If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample
-
代码段中:黑色粗体部分分不清楚,或用其他颜色表示,谢谢。
杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............
那是我用一般的Button举一个例子。这是反射内部代码源码部分,你直接看不到的。
我的意思是: 如果你的方法是绑定到某个事件上的,那么应该先执行诸如触发事件的代码,以便触发事件,随后执行你的代码,最终完成整个事件的处理。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 杲大盛 2013年9月11日 2:40