【问题标题】:UWP IOT-Core App System.Exception: The application called an interface that was marshalled for a different threadUWP IOT-Core App System.Exception:应用程序调用了为不同线程编组的接口
【发布时间】:2019-07-25 16:19:28
【问题描述】:

我无法使用在 Windows 10 IoT Core 上运行的应用。除了通过 JSON 创建 CSV 文件并应该将其作为电子邮件发送的类之外,所有类都工作正常。

当代码到达“ReturnToMainPage()”函数时,抛出异常“System.Exception: The application called an interface that was marshalled for a different thread”。

“有趣”的是,邮件正在发送,我收到了,但程序在发送电子邮件后不会切换回主页。

这是类的代码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using EASendMail;

namespace PratschZahlstation
{
    public sealed partial class MailChoice : Page
    {
        private TextBlock _headerText;
        private ComboBox _mailComboBox;
        private Button _execute;
        private Button _abort;
        private EnDecode _coder = EnDecode.get_EnDecodeSingleton();
        private string _mailto = null;

        public MailChoice()
        {
            this.InitializeComponent();
            Init();
        }

        private void Init()
        {
            _headerText = HeaderText;
            _mailComboBox = MailAdresses;
            _mailComboBox.Items.Add("---");
            _mailComboBox.Items.Add("dummy@mail.com");
            _mailComboBox.SelectedIndex = 0;
            _execute = DoFunction;
            _abort = DoExit;
        }

        private void DoFunction_Click(object sender, RoutedEventArgs e)
        {
            string selectedMail = this._mailComboBox.SelectedItem.ToString();

            if(selectedMail == "---")
            {
                _headerText.Text = "Bitte eine Emailadresse aus der Liste auswählen.";
            }
            else
            {
                _headerText.Text = "CSV wird erstellt und per Mail versendet!";
                _execute.IsEnabled = false;
                _abort.IsEnabled = false;
                _mailComboBox.IsEnabled = false;
                _mailto = selectedMail;
                DateTime date = DateTime.Now;
                string strippedDate = date.ToString("yyyy-MM-dd") + " 00:00:01";

                GetDataForCSV(strippedDate);
            }
        }

        private async void GetDataForCSV(string dateAsString)
        {
            string correctedDate = "2019-07-01 00:00:01";//dateAsString;
            string date = _coder.Base64Encode(correctedDate);

            HttpClient _client = new HttpClient();
            Uri _uri = new Uri("URI TO JSON-API");
            _client.BaseAddress = _uri;
            var request = new HttpRequestMessage(HttpMethod.Post, _uri);

            var keyValues = new List<KeyValuePair<string, string>>();
            keyValues.Add(new KeyValuePair<string, string>("mode", "10"));
            keyValues.Add(new KeyValuePair<string, string>("date", date));

            request.Content = new FormUrlEncodedContent(keyValues);

            var response = await _client.SendAsync(request);

            string sContent = await response.Content.ReadAsStringAsync();

            keyValues = null;

            if (sContent != null)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(sContent);
                string json = Encoding.UTF8.GetString(bytes);

                if (!json.Contains("success"))
                {
                    List<CSV_SQL_Json_Object> _Json = JsonConvert.DeserializeObject<List<CSV_SQL_Json_Object>>(json);
                    response.Dispose();
                    request.Dispose();
                    _client.Dispose();
                    if (_Json.Count == 0)
                    {

                    }
                    else
                    {
                        CreateCSV(_Json);
                    }
                }
                else
                {
                    List<JSON_Status> _Json = JsonConvert.DeserializeObject<List<JSON_Status>>(json);
                    _headerText.Text = "Es ist der Folgender Fehler aufgetreten - Errorcode: \"" + _coder.Base64Decode(_Json[0].success) + "\"\r\nFehlermeldung: \"" + _coder.Base64Decode(_Json[0].message) + "\"";

                        _Json.Clear();
                        response.Dispose();
                        request.Dispose();
                        _client.Dispose();
                }
            }
        }

        private async void CreateCSV(List<CSV_SQL_Json_Object> contentForCSV)
        {
            DateTime date = DateTime.Now;
            string csvName = date.ToString("yyyy-MM-dd") + ".csv";

            StorageFolder storageFolder = KnownFolders.MusicLibrary;
            StorageFile csvFile = await storageFolder.CreateFileAsync(csvName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);

            await FileIO.WriteTextAsync(csvFile, "Column1;Column2;Column3;Column4;\n");

            foreach (var item in contentForCSV)
            {
                await FileIO.AppendTextAsync(csvFile, _coder.Base64Decode(item.Object1) + ";" + _coder.aesDecrypt(_coder.Base64Decode(item.Object2)) + ";" + _coder.aesDecrypt(_coder.Base64Decode(item.Object3)) + ";" + _coder.aesDecrypt(_coder.Base64Decode(item.Object4)) + "\n");
            }

            SendEmail(_mailto, csvName);
        }

        private async void SendEmail(string mailto, string csvName)
        {
            try
                {
                SmtpMail oMail = new SmtpMail("Mail");
                SmtpClient oSmtp = new SmtpClient();

                oMail.From = new MailAddress("noreply@dummy.com");

                oMail.To.Add(new MailAddress(mailto));

                oMail.Subject = "The Subject";

                oMail.HtmlBody = "<font size=5>MailText</font>";

                StorageFile file = await KnownFolders.MusicLibrary.GetFileAsync(csvName).AsTask().ConfigureAwait(false);

                string attfile = file.Path;

                Attachment oAttachment = await oMail.AddAttachmentAsync(attfile);

                SmtpServer oServer = new SmtpServer("mail.dummy.com");

                oServer.User = "dummyuser";
                oServer.Password = "dummypass";
                oServer.Port = 587;
                oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
                await oSmtp.SendMailAsync(oServer, oMail);

                }
                catch (Exception ex)
                {
                    string error = ex.ToString();
                    _abort.IsEnabled = true;
                }

            ReturnToMainPage(); //This is where the Error Happens
        }

        private void ReturnToMainPage()
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        private void DoExit_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
    }
}

【问题讨论】:

    标签: c# uwp windows-10-iot-core


    【解决方案1】:

    这可能是线程问题。只能在主线程上导航。

    您可能想尝试编组调用

    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () =>
        {
            // Your UI update code goes here!
        }
    );
    

    来源:

    The application called an interface that was marshalled for a different thread - Windows Store App

    【讨论】:

      【解决方案2】:

      就像Tobonaut说的,你可以用Dispatcher.RunAsync来调用Navigation,它成功了。

      但你的问题可能不是这个。

      我复制了你的代码,复现了你的问题,发现你的文件读写调用有问题:

      // Your code
      StorageFile csvFile = await storageFolder.CreateFileAsync(csvName, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);
      
      StorageFile file = await KnownFolders.MusicLibrary.GetFileAsync(csvName).AsTask().ConfigureAwait(false);
      

      如果您删除.AsTask().ConfigureAwait(false),导航将起作用。

      最好的问候。

      【讨论】:

      • 谢谢。那成功了。现在我只需要弄清楚应用程序的证书有什么问题,因为它不会签名 xD
      猜你喜欢
      • 2023-03-20
      • 2018-12-26
      • 2017-12-18
      • 2013-10-20
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多