C#,Gmap.net:使用Gmap.net图书馆在Google地图上绘制5个标记时生成异常 [英] C#,Gmap.net :Generating exception while plotting 5 markers on google map using Gmap.net liabrary

查看:140
本文介绍了C#,Gmap.net:使用Gmap.net图书馆在Google地图上绘制5个标记时生成异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gmap.net库在三角测量算法在谷歌地图上进行计算后,在特定的让步处绘制标记,当我为两个标记工作时,它的效果很好,但是当我尝试绘制5个标记时,它正在显示我例外

i am using Gmap.net library to plot markers at a particular let-long coming after calculation made by triangulation algo on google maps,when i worked for two markers its works fine but when i tries to plot 5 markers its is showing me exception

类型为'System.InvalidOperationException'的未处理异常 发生在mscorlib.dll

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

其他信息:集合已修改;枚举运算 可能无法执行.

Additional information: Collection was modified; enumeration operation may not execute.

我在做什么,我为5个标记创建了5个线程,这些标记正在地图上绘制.

what i am doing ,i have created 5 threads for five markers ,which are plotting on the map.

  1. 当标记物正在绘制时,如果我进行了大量的放大操作,则缩小 它显示异常.
  2. 在绘图时,我再次移动地图以显示异常.
  1. while markers are getting plotting ,if i do lots of zoom in zoom out it show exception.
  2. while plotting i move the map again it show exception.

我无法弄清楚为什么会发生.即使它没有显示任何行号.这是错误的快照. 请帮忙. 这是要在地图上绘制的代码,包括添加标记和路线,我对所有5个具有相同标记和路线名称的标记使用相同的功能

i am unable to figure out why it is happening.even it is not showing me any line no. here is the snapshot of error. please help. here is the code to plot on map includes adding markers and routes ,i have use same function for all 5 markers with different marker and routes name

void plot5(double temp_lat5, double temp_long5, string temp_date5, string temp_time5, string temp_bty_value5)
        {

                try
                {

                    //GMapMarker marker5 = new GMarkerGoogle(new PointLatLng(temp_lat5, temp_long5), GMarkerGoogleType.lightblue);
                    GMapMarker marker5 = new GMarkerGoogle(new PointLatLng(temp_lat5, temp_long5), new Bitmap(Properties.Resources.image12));
                    gmap.Overlays.Add(markers5);   // overlay added
                    Thread.Sleep(100);
                    markers5.Markers.Add(marker5);  //marker added 
                    marker5.ToolTipText = "AT CARD 05" + "\n" + "HIGH PRIORITY" + "\n" + temp_date5 + "\n" + temp_time5 + "\n" + temp_bty_value5 + "%"; //text to be displayed on the marker
                                                                                                                                                        // text properties
                    marker5.ToolTip.Fill = Brushes.Bisque;
                    marker5.ToolTip.Foreground = Brushes.White;
                    marker5.ToolTip.Stroke = Pens.Black;
                    marker5.ToolTip.TextPadding = new Size(20, 20);



                    if (count5 != 0)
                    {
                        List<PointLatLng> points5 = new List<PointLatLng>();
                        points5.Add(new PointLatLng(temp_lat5, temp_long5));
                        points5.Add(new PointLatLng(lat5, long5));
                        GMapRoute route5 = new GMapRoute(points5, "walk05");
                        route5.Stroke = new Pen(Color.Aquamarine, 3);

                        gmap.Overlays.Add(routes5); // first overlays then routes or markers
                        routes5.Routes.Add(route5);
                    }
                    count5++;
                    lat5 = temp_lat5;
                    long5 = temp_long5;
                }
                catch (Exception hy)
                { MessageBox.Show(hy.ToString()); }

这就是m调用不同线程/任务(两者都尝试过)的方式

that's how m calling the different threads /tasks(tried with both)

  private   void button1_Click(object sender, EventArgs e)// Creating and startting threads for 5 diffrent at cards
        {
            try
            {
                if (m_parentform.update_flag_data == 1)
                {
                    if (list_at.Count != 0)
                    {
                        for (int y = 0; y < list_at.Count; y++)
                        {
                            for (int x = 0; x < m_parentform.at_cards_serials.Count; x++)
                            {
                                if (list_at[y] == m_parentform.at_cards_serials[x])

                                {
                                    try
                                    {
                                        int index = m_parentform.at_cards_serials.IndexOf(m_parentform.at_cards_serials[x]);
                                        switch (index)
                                        {
                                            case 0:
                                                {
                                                    Task lat_longthread1 = new Task(new System.Action(custom01));
                                                    lat_longthread1.Start();
                                                   // await lat_longthread1;

                                                   // Thread lat_longthread1 = new Thread(new ThreadStart(custom01));
                                                   // lat_longthread1.Start();

                                                    break;
                                                }

                                            case 1:
                                                {
                                                    Task lat_longthread2 = new Task(new System.Action(custom02));
                                                    lat_longthread2.Start();
                                                   // await lat_longthread2;
                                                    //Thread lat_longthread2 = new Thread(new ThreadStart(custom02));
                                                    //lat_longthread2.Start();
                                                    break;
                                                }

                                            case 2:
                                                {
                                                    Task lat_longthread3 = new Task(new System.Action(custom03));
                                                    lat_longthread3.Start();
                                                  //  await lat_longthread3;
                                                    //Thread lat_longthread3 = new Thread(new ThreadStart(custom03));
                                                    //lat_longthread3.Start();
                                                    break;
                                                }
                                            case 3:
                                                {
                                                    Task lat_longthread4 = new Task(new System.Action(custom04));
                                                    lat_longthread4.Start();
                                                   // await lat_longthread4;
                                                    //Thread lat_longthread4 = new Thread(new ThreadStart(custom04));
                                                    //lat_longthread4.Start();
                                                    break;
                                                }
                                            case 4:
                                                {
                                                    Task lat_longthread5 = new Task(new System.Action(custom05));
                                                    lat_longthread5.Start();
                                                  //  await lat_longthread5;
                                                    //Thread lat_longthread5 = new Thread(new ThreadStart(custom05));
                                                    //lat_longthread5.Start();
                                                    break;
                                                }
                                        }

                                    }
                                    catch (Exception ty)
                                    { MessageBox.Show(ty.ToString()); }
                                }
                            }
                        }
                    }
                    else
                    { MessageBox.Show("select at cards to plot"); }

                }
                else
                { MessageBox.Show("flag is not updated"); }
            }
            catch (Exception jo)
            { MessageBox.Show(jo.ToString()); }

更改:以下是我所做的更改,添加了绘图功能(如上所述),对于关键部分中的所有5个标记,仍然存在相同的问题.

CHANGES:following is the change i made ,added the plot function(described above),for all 5 markers in critical section ,still issue is same.

void marker_selection(int plot_no)
        {
            mut2.WaitOne();
            int marker_no = plot_no;
            switch (marker_no)
                {

                case 1:
                    {
                        plot(one1, two1, date[0], time[0], bty[0]);  // not sending bty part 2 which indicates milli volt value 
                        break;
                    }
               case 2:
                    {
                        plot2(one2, two2, date2[0], time2[0], bty2[0]);  // sending only part one of bty
                        break;
                    }
                case 3:
                    {
                        plot3(one3, two3, date3[0], time3[0], bty3[0]);  // sending only part one of bty
                        break;
                    }
                case 4:
                    {
                        plot4(one4, two4, date4[0], time4[0], bty4[0]);  // sending only part one of bty
                        break;
                    }
                case 5:
                    {
                        plot5(one5, two5, date5[0], time5[0], bty5[0]);  // sending only part one of bty
                        break;
                    }
            }
            mut2.ReleaseMutex();
        } 

推荐答案

因此,您提到要运行单独的线程来添加标记和路由.这可能会导致线程问题. UI线程尝试绘制标记,因此在标记列表上进行迭代.当另一个线程将标记添加到当前正在绘制的标记集合中时,迭代器将变得无效,因此将引发异常.

So you mentioned you run separate threads to add the markers and routes. This can result in threading issues. The UI thread tries to draw your markers and therefore iterates over the lists of markers. When another thread adds a marker to the collection of markers that are being currently drawn, then the iterator becomes invalid, hence the exception is thrown.

解决方案:确保标记的添加以UI同步的方式进行.

The solution: Make sure the adding of the markers happens in an UI synchronized manner.

作为验证上述假设的简单第一种方法,请尝试不使用线程的代码,然后了解如何使您的线程与主线程同步.基本上,最终AddMarker()必须在主线程上发生.

As a simple first approach to validate the assumption above try your code without threading and then read how to synchronize your threads with the main thread. Basically the eventual AddMarker() has to happen on the main thread.

修改 您需要在InvokeRequiredBeginInvoke的帮助下应用同步,如在此链接中所示.看看他们如何更新文本控件(一个作为gmap实例的uni元素).为此,要起作用,标记的实际添加必须在该动作/委托中进行.让我们尝试从那里拿走它,让我们知道您能走多远.

Edit You'll need to apply synchronisation as demonstrated in this link with the help of InvokeRequired and BeginInvoke. Have a look how they update the text control, an uni element as the gmap instance. For this then to work the actual add of the markers has to happen within that action/delegate. Let's try to take it from there and let us know how far you get, please.

这篇关于C#,Gmap.net:使用Gmap.net图书馆在Google地图上绘制5个标记时生成异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆