C#,Gmap.net:使用 Gmap.net 库在谷歌地图上绘制 5 个标记时生成异常

2023-11-30

我正在使用 Gmap.net 库在谷歌地图上的三角测量算法进行计算后在特定的时间绘制标记,当我为两个标记工作时,它工作正常,但当我尝试绘制 5 个标记时,它向我显示异常

“System.InvalidOperationException”类型的未处理异常 发生在 mscorlib.dll 中

附加信息:集合已修改;枚举操作 可能不执行。

我正在做的事情是,我为五个标记创建了 5 个线程,这些标记绘制在地图上。

  1. 当标记正在绘制时,如果我进行大量放大缩小 它显示异常。
  2. 在绘制时,我再次移动地图,它显示异常。

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.enter image description here please help. EDIT: 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()); }

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

  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 个标记,问题仍然相同。

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 线程尝试绘制标记,因此会迭代标记列表。当另一个线程将标记添加到当前正在绘制的标记集合中时,迭代器将变得无效,从而引发异常。

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

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

Edit您需要应用同步,如图所示在这个链接中在...的帮助下InvokeRequired and BeginInvoke。看看他们如何更新文本控件,将 uni 元素作为 gmap 实例。为此,标记的实际添加必须在该操作/委托内发生。让我们尝试从那里开始,请告诉我们您已经走了多远。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#,Gmap.net:使用 Gmap.net 库在谷歌地图上绘制 5 个标记时生成异常 的相关文章

随机推荐

  • 在 C# 中按键模拟按钮单击

    好的 我正在制作一个 Tic Tac Toe 游戏来帮助我学习 C 我正在尝试为其添加一些功能 因此我希望人们能够在计算机上使用数字键盘来模拟单击按钮 这是我所拥有的 但当我使用数字键盘时 按钮不会单击 你们中有人能看出原因吗 start
  • 可变参数模板复杂继承生成

    尝试了一下可变参数模板 看看可以用它们做什么 我发现自己想知道一些事情 假设我有一个类 它可以将其他几个类作为模板参数 每个类都有一个嵌套类 我们称之为nested class template
  • Nil 与预期的参数类型选择器不兼容

    在从 Swift 2 3 转换为 Swift 3 时 我收到以下代码行的错误 var contactButton UIBarButtonItem return self CustomRightItem icon nav nls contac
  • 如何避免插入重复记录

    我正在使用 Laravel 5 6 29 if HarmFlag where post id postId gt where harm id harm id gt get gt isEmpty HarmFlag create post id
  • htaccess - 如果末尾有问号,则剪掉问号

    有这么一段代码
  • 成功解析 SimpleXMLElement 与“false”的比较,返回“true”

    我在 simplexml 评估中遇到了一个非常尴尬且具体的问题 代码 simplexml simplexml load string xmlstring var dump simplexml var dump simplexml false
  • 刷新 div 从数据库检索信息而不刷新整个页面

    我想刷新divauto每 2 秒一次 无需重新加载页面 我该如何使用 jquery 执行此操作我确实尝试了一些解决方案 但它们不起作用 如果可以提供帮助那就太好了 对话 php
  • 何时正确使用 Task.Run 以及何时仅使用 async-await

    我想询问您对何时使用正确架构的看法Task Run 我在 WPF NET 4 5 中遇到了缓慢的 UI 应用程序 使用 Caliburn Micro 框架 基本上我正在做 非常简化的代码片段 public class PageViewMod
  • 打印传递变量名称的函数

    我最初的目标是创建一个函数来打印给定对象的类型和内存地址 为了尽可能通用 我还想包含变量名称 如下所示 gt gt gt a 10 gt gt gt print type addr a a int 0x13b8080 为此 我需要知道传递给
  • Unicode 命名的文件夹显示?在 wscript 提示符下

    我遇到 Unicode 命名文件夹的问题 当我将文件夹拖到脚本中时 它没有正确显示文件夹的路径 简单的 VBScript 这只是其中的一部分 Dim Wshso Set Wshso WScript CreateObject WScript
  • Python argparse:强制列表项唯一

    能够使用以下方式验证列表项choices servers下面很好 servers ApaServer BananServer GulServer SolServer RymdServer SkeppServer HavsServer Sov
  • Android 从顶部滑动抽屉?

    有什么办法可以让抽屉从上滑到下吗 我找到了一个简单的方法来做到这一点 您所要做的就是为slidingDrawer 内容和手柄设置180 的旋转 通过一个例子更容易理解 所以看看我做了什么 首先 我将向您展示我的旧 SlidingDrawer
  • 将可可粒放入可可中

    如何将笔尖加载到另一个窗口内 我尝试了 initWithWindowName if mmController NULL mmController mainMenu alloc initWithWindowNibName mainMenu m
  • 在 CasperJS 的表单之外使用选择

    我想选择一个条目进入选择 我如何使用 CasperJS 做到这一点 您需要了解的一些信息 我无法修改页面 我的任务只是编写测试 选择标记不包含在表单中 我无法使用document querySelector MY SELECT select
  • Angular 2 节点波本错误

    ERROR in node modules css loader sourceMap false importLoaders 1 node modules postcss loader lib ident postcss sourceMap
  • 去掉标签,但保留第一个

    例如我怎样才能保留第一个img标记但剥离所有其他的 来自 HTML 字符串 example p some text img src aimage jpg alt desc width 320 height 200 img src aimag
  • iPhone 上的 Lua

    我正在尝试在 iPhone 上运行时加载 Lua 脚本 有可能这样做吗 我该如何开始使用 Lua 我找不到东西 我唯一能找到的是这应该是可能的 但我想知道如何实现 查看科罗娜SDK
  • 熊猫留在原地

    我有一个大数据框df和一个小数据框df right有 2 列a and b 我想做一个简单的左连接 查找a无需复制df 我想出了这段代码 但我不确定它有多强大 dtmp pd merge df a df right on a how lef
  • SKSpriteNode 框架偏离

    我正在尝试从 UIBezierPath 创建一个开放圆 并将其转换为 SKShapeNode 稍后将其转换为 SKSpriteNode 我遇到了一个问题 我不知道如何缩小精灵 而它的线宽没有缩小 您可以在这里查看解决方案 调整精灵大小而不缩
  • C#,Gmap.net:使用 Gmap.net 库在谷歌地图上绘制 5 个标记时生成异常

    我正在使用 Gmap net 库在谷歌地图上的三角测量算法进行计算后在特定的时间绘制标记 当我为两个标记工作时 它工作正常 但当我尝试绘制 5 个标记时 它向我显示异常 System InvalidOperationException 类型