3
Answers

Problems with Delegates and Invoking

Jon

Jon

14y
2.9k
1
I'm working on a port scanner that constantly displays the status of a game server, but for some reason I keep getting this error : http://i47.tinypic.com/vpgkl4.png

I'm pretty new to the c sharp language, and I've been looking around online about delegates, but I seem to have messed up somewhere after following an example.

Everything works fine until I decide to terminate my program, that's when the error comes up, and visual studio does not stop debugging and unlocking the source files automatically.(i have to hit ctrl+f5 manually). I'd be grateful if someone would point out my error(s).
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace AfterPatch_SC__Launcher
{
public partial class frm_StatusChecker : Form
{
Thread WorldServerStatusThread;

public frm_StatusChecker()
{
InitializeComponent();
}

private void frm_StatusChecker_Load(object sender, EventArgs e)
{
this.WorldServerStatusThread = new Thread(new ThreadStart(this.WorldServerStatus));
this.WorldServerStatusThread.Start();
}

delegate void SetTextCallback(string Text);
private void WorldServerStatus()
{
while ( true )
{
try
{
Thread.Sleep(1000);
TcpClient CheckWorldServer = new TcpClient();
CheckWorldServer.Connect("63.251.217.4", 8484);
if (this.label_WorldServerStatus.InvokeRequired)
{
SetTextCallback A1 = new SetTextCallback(SetText);
this.Invoke(A1, new object[] { "ONLINE" });
}
else
{
this.label_WorldServerStatus.Text = "ONLINE";
}
}
catch
{
if (this.label_WorldServerStatus.InvokeRequired)
{
SetTextCallback A2 = new SetTextCallback(SetText1);
this.Invoke(A2, new object[] { "OFFLINE" });
}
else
{
this.label_WorldServerStatus.Text = "OFFLINE";
}
}
}
}

private void SetText(string StatusOnline)
{
this.label_WorldServerStatus.Text = "ONLINE";
}

private void SetText1(string StatusOffline)
{
this.label_WorldServerStatus.Text = "OFFLINE";
}
}
}
I apologize beforehand for how messy or nooby my coding may be..

Answers (3)