简体   繁体   English

从 C# VS2013 WPF 通过多线程将图像添加到地图的错误

[英]error of adding an image to a map by multithreading from C# VS2013 WPF

I would like to add an image to a map based on OpenStreetMap from C# VS2013 WPF desktop application.我想将图像添加到基于 C# VS2013 WPF 桌面应用程序的 OpenStreetMap 的地图。

But, the time of generating the image may be long so I need to use multithreading to do this.但是,生成图像的时间可能很长,所以我需要使用多线程来做到这一点。 In main thread, I created a new thread to draw and add the image to the WPF desktop application.在主线程中,我创建了一个新线程来绘制图像并将其添加到 WPF 桌面应用程序。

In single thread, it works well.在单线程中,它运行良好。 But, in multithreading, no image can be added to the map.但是,在多线程中,不能将图像添加到地图中。

The code is as follows:代码如下:

 using System.ComponentModel;
 private BackgroundWorker worker_addImage;
 public MainWindow()
    {
        InitializeComponent();
        worker_addImage = new BackgroundWorker();
        worker_addImage.DoWork += (s, a) =>
        {
            MessageBox.Show("the worker started \r\n");
            DrawImage();
        };
        _worker_addImage.ProgressChanged += _worker_ProgressChanged;
        _worker_addImage.RunWorkerCompleted += (s, a) =>
        {
            MessageBox.Show("image added \r\n");
            AddImage();
        };
    }

    private void ShowImage_Clicked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Show Image \r\n");
        if (worker_addImage.IsBusy == false)
            worker_addImage.RunWorkerAsync();
    }
    // why this progress function is not executed ? 
    private static void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        MessageBox.Show(e.ProgressPercentage.ToString());
    }

UPDATE更新

This is the code that add image to the map.这是将图像添加到地图的代码。

    private void AddImage()
    {
        Uri uri = new Uri(resultImageString, UriKind.Absolute);
        System.Windows.Media.ImageSource imgSource = new BitmapImage(uri);
        // the image generated by DrawImage() is saved in this URI. I can 
        // open and see it. But, it cannot be added to the map. But, if I
        // used single thread, no problems.
        mapImage.Source = imgSource;  
    }

In DrawImage().在 DrawImage() 中。 I saved the image in a local folder as a png file.我将图像作为 png 文件保存在本地文件夹中。 But, in AddImage(), I was told that the png file cannot be accessed because another thread or process was using it.但是,在 AddImage() 中,我被告知无法访问 png 文件,因为另一个线程或进程正在使用它。 Why ?为什么 ?

Is that due to that RunWorkerCompleted handler is invoked in the UI thread but the png file was being used by the "worker_addImage" thread ?这是因为在 UI 线程中调用了 RunWorkerCompleted 处理程序,但“worker_addImage”线程正在使用 png 文件吗?

      using (MemoryStream memory = new MemoryStream())
      {
               using (FileStream fs = new FileStream(res, FileMode.Create, FileAccess.ReadWrite))
                {
                    myImage.Save(memory, ImageFormat.Png);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
      }

You are missing RunWorkerAsync() on worker_addImage.您在 worker_addImage 上缺少 RunWorkerAsync()。 Also message box should be marshalled as your process is not in the main UI thread.由于您的进程不在主 UI 线程中,因此还应编组消息框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#WPF多线程无法从VS2013中的Dispatcher.BeginInvoke返回实际值 - C# WPF multithreading cannot return the actual value from Dispatcher.BeginInvoke in VS2013 基于来自 C# VS2013 WPF 的 OpenStreetMap 在地图上显示 100 万个带有标记的位置 - show 1 million locations with markers on a map based on OpenStreetMap from C# VS2013 WPF 使用XAML Map Control在基于C#VS2013 WPF的OpenStreetMap的地图上添加热图图层 - using XAML Map Control to add a heatmap layer on a map based on OpenStreetMap from C# VS2013 WPF Win7上的C#WPF VS2013无法在文本框中显示所选文件名 - the selected file name cannot be displayed in the text box from C# WPF VS2013 on win7 如何在VS2013中从WinForm生成日志(C#) - How to generate logs from a winform in VS2013(c#) 将标识符从Color更改为Color C#/ VS2013 - Change identifier from Color to Colour C#/VS2013 在VS2013中使用Mathdotnet在C#中的beta发行版 - use beta distribution from mathdotnet in C# in VS2013 尝试从VS2013 C#程序调用DELPHI XE2 DLL时出错 - Error in trying to call a DELPHI XE2 DLL from VS2013 C# program “报告的报告定义” <vsprojectname> VS2013 WPF / C#应用程序中未指定.report1.rdlc&#39;错误 - 'the report definition for report '<vsprojectname>.report1.rdlc' has not been specified' error in VS2013 WPF/C# application VS2013上C#的VTK包装 - VTK wrap for C# on VS2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM