I've been trying to modify some GUI elements before/after a TCP connection that i'm trying to execute synchronously and assynchronously.
public async void ConnectAsync(String h, String p, String n)
{
if (connected)
return;
try
{
hostname = new HostName(h);
port = p;
nickname = n;
await sock.ConnectAsync(hostname, port);
connected = true;
if (Connected != null)
Connected(this, EventArgs.Empty);
}
catch (Exception e)
{
sock.Dispose();
hostname = null;
port = null;
nickname = null;
if (ConnectionFailed != null)
ConnectionFailed(this, EventArgs.Empty);
}
}
This method above is called by the GUI class (code below):
private void ConnectButtonClicked(object sender, RoutedEventArgs e)
{
string nickname;
if (Bar.Visibility == Windows.UI.Xaml.Visibility.Visible)
Bar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
if (Status.Visibility == Windows.UI.Xaml.Visibility.Collapsed)
Status.Visibility = Windows.UI.Xaml.Visibility.Visible;
qc.ConnectionFailed += new ConnectionFailedEventHandler(ConnectionFailedEventHandler);
qc.Connected += new ConnectedEventHandler(ConnectedEventHandler);
nickname = Nickname.Text;
/* HERE */
Task.Run(() => qc.ConnectAsync("irc.quakenet.org", "6667", nickname));
updateStatus("Connecting...");
ConnectButton.IsEnabled = false;
Nickname.IsEnabled = false;
ProgLanguages.IsEnabled = false;
}
See that the method raises two different events..
If i call this method like this code, with Task.Run(..), those events are raised and when they are handled, the code tries to modify the GUI by this thread and an exception is thrown.
If i call the method without the Task.Run(..), the GUI freezes and i can't modify the elements to show that it's "Connecting" and something like that.
Any idea how can i do this?