versucht, meine kleine Windows-Formular-App fertig zu schreiben, die ich in C# geschrieben habe. Ich baue es ohne Fehler für die Variable "e", die nie verwendet wird.
Wenn ich das Programm nach dem Erstellen ausführen, passiert nichts und in der Ereignisanzeige erhalte ich einen .net Laufzeitfehler (unten eingefügt). Code:C# /. Net Fehler System.NullreferenceException
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Thread thread;
public Form1()
{
InitializeComponent();
richTextBox1.Text = "Epic pinger";
Form1.ActiveForm.FormClosing += new FormClosingEventHandler(Form1_Closing);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (thread != null)
{
thread.Abort();
}
}
public void ping()
{
string textb1 = textBox1.Text;
try
{
while (true)
{
if (textb1 == "")
{
return;
}
using (Ping p = new Ping())
{
//Konfigurerings boksene int/strings
string msStørreKonf2 = msKonfig2.Text;
string msStørre = textBox2.Text;
int msStørst = 100;
int msKonf2t = 100;
Int32.TryParse(msStørreKonf2, out msKonf2t);
Int32.TryParse(msStørre, out msStørst);
this.Invoke((MethodInvoker)delegate
{
try
{
listView1.Items.Add(p.Send(textBox1.Text).RoundtripTime.ToString() + "ms\n");
if (p.Send(textBox1.Text).RoundtripTime > msStørst) { Console.Beep(1500, 400); } //konfig boks 1
if (p.Send(textBox1.Text).RoundtripTime > msKonf2t) { Console.Beep(6500, 700); } //konfig boks 2
listView1.EnsureVisible(listView1.Items.Count - 1);
}
catch (PingException e)
{
Console.Beep(5000, 100);
richTextBox1.AppendText("Error med ping addressen eller pip konfigurasjon! ");
}
});
//Søvetid, henter input fra tidsBox.
string tidPingString = tidsBox.Text;
int tidPing = 1000;
Int32.TryParse(tidsBox.Text, out tidPing);
Thread.Sleep(tidPing);
// testing:
}
}
}
catch (ThreadAbortException e)
{
// Ska vær blank!
}
}
private void button1_Click(object sender, EventArgs e)
{
if (thread != null && thread.IsAlive)
{
thread.Abort();
listView1.Items.Add("|");
}
else
{
thread = new Thread(new ThreadStart(ping));
thread.Start();
}
}
Fehler XML-Ansicht:
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name=".NET Runtime" />
<EventID Qualifiers="0">1026</EventID>
<Level>2</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2016-06-30T07:40:42.499533800Z" />
<EventRecordID>40977</EventRecordID>
<Channel>Application</Channel>
<Computer>computername.domainname</Computer>
<Security />
</System>
- <EventData>
<Data>Application: EndlessP.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException at WindowsFormsApplication2.Form1..ctor() at WindowsFormsApplication2.Program.Main()</Data>
</EventData>
</Event>
Es ist gelöst, die Kommentare zu sehen, die Lösung zu sehen. Wenn ich kann, werde ich versuchen, die Erklärung hier hinzuzufügen, warum es passiert ist.
Realisierte einige Formatierungsfehler in der Frage. Sorry, meine zweite Post hier – Zeay
Wissen Sie, wo genau die Ausnahme geworfen wird? –
tue ich nicht, sorry. Momentan liest er Renes Antwort. @ Nitro.de – Zeay