MessageBox未显示在BackgroundWorker中[关闭]

问题描述 投票:-3回答:1

我一直在使用打印机驱动程序,但是,该驱动程序大多数时候都在backgroung上工作,只有几个用于配置和调试带有测试页的UI。

在代码的某一点上,我有一个后台工作人员对API进行了一些调用,我需要根据API的响应进行一些操作,例如show MessageBoxs或使用System.Diagnostics.Process.Start( )

当我在调试用户界面中测试代码时,它可以正常工作,但是当我测试驱动程序进行正常打印时(因此它运行了白色的用户界面),所有的MessageBox均不起作用,并且System.Diagnostics.Process.Start()也不起作用

我以为这个问题可能与没有UI的事实有关

这里是BackgroundWorker代码:

 private async void APIPrint(object sender, DoWorkEventArgs e)
        {
            long length = new FileInfo(_currentOutputFile).Length;
            try
            {
                string url = SettingsHelper.Settings.APISettings.Url;
                string api = SettingsHelper.Settings.APISettings.Version;
                string coin = SettingsHelper.Settings.APISettings.Coin;
                string network = SettingsHelper.Settings.APISettings.Network;
                string address = SettingsHelper.Settings.BSVAddressSettings.Address;
                string privkey = SettingsHelper.Settings.BSVAddressSettings.PrivKey;
                string call = url + "/" + api + "/" + coin + "/hasbalance/" + address + "/" + length;
                var json = new WebClient().DownloadString(call);
                Result m = JsonConvert.DeserializeObject<Result>(json);
                if (m.exits)
                {
                    if (m.hasbalance)
                    {
                        using (var content = new MultipartFormDataContent())
                        {
                            var dataContent = new FormUrlEncodedContent(new[]
                            {
                          new KeyValuePair<string, string>("Key", "Value")
                       });
                            content.Add(dataContent);

                            using (var fileStream = File.OpenRead(_currentOutputFile))
                            using (var fileStreamContent = new StreamContent(fileStream))
                            {
                                fileStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                                {
                                    Name = "datafile",
                                    FileName = Path.GetFileName(_currentOutputFile)
                                };
                                fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                                content.Add(fileStreamContent);
                                using (var httpClient = new HttpClient())
                                {
                                    var httpResponse = await httpClient.PostAsync(url + "/" + api + "/" + coin + "/" + network + "/upload/" + privkey, content);
                                    var responseContent = await httpResponse.Content.ReadAsStringAsync();
                                    ResultPrint result = JsonConvert.DeserializeObject<ResultPrint>(responseContent);
                                    if (result.status)
                                    {

                                        System.Diagnostics.Process.Start(result.tx);
                                    }
                                    else
                                    {
                                       MessageBox.Show(result.message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (m.url == null || m.url == "" || m.url == " " || m.email == null || m.email == "" || m.email == " ")
                        {
                            MessageBox.Show(m.message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                        }
                        MessageBox.Show(m.message + " " + m.url + " " + m.email, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    }

                }
                else
                {
                    if (m.url == null || m.url == "" || m.url == " ")
                    {
                        MessageBox.Show(m.message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    }
                    else
                    {
                        if (MessageBox.Show(m.message, "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(m.url);
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Could not connect to server. Check your internet connection and wallet/API Configuration or try later.");
            }
        }

编辑:messageBox都不起作用。

c# .net wpf
1个回答
-1
投票

您必须仅在主线程中而不是在工作线程中调用消息框。

在您的情况下,您必须执行一些从后台工作人员到主线程的数据报告,然后调用消息框。

更新:您可以通过ProgressChangedRunWorkerCompleted事件处理程序进行更新和显示消息,因为它们与UI线程同步。

© www.soinside.com 2019 - 2024. All rights reserved.