[英]An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code
[英]WinForms - System.Reflection.TargetInvocationException in mscorlib.dll when no internet connection is available on application start
我制作了一個簡單的桌面應用程序(Winform,在 Visual Studio 2015 中,.NET 版本 4.5.2),它偵聽傳入的 pusher.com 通知,並根據通知包含的內容執行各種操作。 但是,互聯網連接存在一些問題 - 如果啟動時沒有連接,應用程序會因未處理的異常而崩潰:
An unhandled exception of type System.Reflection.TargetInvocationException occurred in in mscorlib.dll
InnerException
是No such host is known
,這是可以理解的,因為沒有互聯網連接。
如果在應用程序運行時連接中斷,代碼的StartPushing()
位將繼續嘗試重新連接,並且不穩定的連接不是(最大的)問題。 在應用程序運行之前它沒有連接。
我還從public Form1()1
刪除了StartPushing()
並將其放置在Form1_Shown(...)
(認為它可能在所有設置之前就被調用了),但這並沒有解決問題 - 應用程序不斷崩潰在啟動時。
這是我的代碼。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using Newtonsoft.Json;
using PusherClient;
namespace PusherWebApps
{
public partial class Form1 : Form
{
private static Pusher _pusher;
public string channel, key;
public string event1, event2;
public string iniName = "PusherWebApps.ini";
public string type;
WebClient client = new WebClient();
public Form1()
{
InitializeComponent();
// https://stackoverflow.com/a/39308022
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
StartPushing();
}
void _pusher_ConnectionStateChanged(object sender, PusherClient.ConnectionState state)
{
fileLogging("Connection state: " + state.ToString());
}
void _pusher_Error(object sender, PusherException error)
{
fileLogging("Pusher Channels Error: " + error.ToString());
}
// Start
public async void StartPushing()
{
iniParams();
_pusher = new Pusher(key);
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
PusherClient.ConnectionState connectionState = await _pusher.ConnectAsync();
Channel _myChannel = await _pusher.SubscribeAsync(channel);
_myChannel.Bind("myevent", (dynamic incoming) =>
{
fileLogging(incoming.ToString());
dynamic tmp = JsonConvert.DeserializeObject(incoming.ToString());
dynamic data = JsonConvert.DeserializeObject(tmp.data.ToString());
dynamic message = JsonConvert.DeserializeObject(data.message.ToString());
dynamic header = JsonConvert.DeserializeObject(message.header.ToString());
type = header.type.ToString();
if (type.ToLower() == event1)
{
// do stuff
}
if (type.ToLower() == event2)
{
// do stuff 2
}
});
}
void _myChannel_Subscribed(object sender)
{
fileLogging("Subscribed!");
}
public void iniParams()
{
bool checkIni = File.Exists(Application.StartupPath + "\\" + iniName);
if(!checkIni)
{
fileLogging("INI missing!");
MessageBox.Show("INI missing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
var MyIniFile = new IniFile(Application.StartupPath + "\\" + iniName);
channel = MyIniFile.Read("channel", "Main").ToString();
key = MyIniFile.Read("key", "Main").ToString();
event1 = MyIniFile.Read("event1", "Main").ToString();
event2 = MyIniFile.Read("event2", "Main").ToString();
}
/************ BEGIN logging **********/
public void fileLogging(string text)
{
// do logging stuff
}
/********** END logging *********/
}
}
最后,我的問題。
我該如何處理這個錯誤?
我知道我可能會在啟動時通知用戶沒有互聯網連接,然后退出應用程序,但我想保持它運行,並讓它繼續嘗試在后台連接。 我還想避免在后台使用單獨的線程和計時器來檢查互聯網連接。
編輯 1
這是調試器指向的部分 - 而不是指向Form1.cs
某處,而是突出顯示Program.cs
這一部分
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // <== this is the one which the debugger highlights as the error
}
}
只需在您的代碼中添加一個 try/catch。 如果應用程序具有 GUI 或類似的東西,並且您希望最終用戶即使在沒有互聯網的情況下也能看到它,您可以將 try/catch 僅添加到 WebClient 部分而不是整個代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.