Skip to content

Commit a33c899

Browse files
committed
Make VNC connect actions asynchronous and update UI state
Refactored the connect button handlers in both WPF and Windows Forms examples to use async/await and run the blocking VNC connect call on a background thread. Added SetConnectingState methods to disable UI controls and update button text and cursor during connection attempts, improving responsiveness and user feedback.
1 parent 7a43377 commit a33c899

8 files changed

Lines changed: 62 additions & 72 deletions

File tree

RemoteViewing.ServerExample/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3535
[assembly: AssemblyDescription("C# VNC server example")]
3636
[assembly: AssemblyProduct("RemoteViewing")]
3737
[assembly: AssemblyTitle("RemoteViewing.ServerExample")]
38+
[assembly: AssemblyFileVersion("1.2.0.0")]
39+
[assembly: AssemblyVersion("1.2.0.0")]
3840
[assembly: ComVisible(false)]
3941
[assembly: Guid("030506a8-328e-47fd-b9b5-b0e7207c2ac9")]

RemoteViewing.WPF.Example/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Window x:Class="RemoteViewing.WPF.Example.MainWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:vnc="clr-namespace:RemoteViewing.WPF;assembly=RemoteViewing.WPF"
4+
xmlns:vnc="https://github.com/lemutec/RemoteViewing"
55
Title="RemoteViewing - Example VNC Client"
66
Width="900"
77
Height="600"

RemoteViewing.WPF.Example/MainWindow.xaml.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3030

3131
using System;
3232
using System.Net.Sockets;
33+
using System.Threading.Tasks;
3334
using System.Windows;
3435
using System.Windows.Input;
3536
using System.Windows.Threading;
@@ -56,7 +57,7 @@ public MainWindow()
5657
_statisticsTimer.Start();
5758
}
5859

59-
private void btnConnect_Click(object sender, RoutedEventArgs e)
60+
private async void btnConnect_Click(object sender, RoutedEventArgs e)
6061
{
6162
if (vncControl.Client.IsConnected)
6263
{
@@ -83,13 +84,15 @@ private void btnConnect_Click(object sender, RoutedEventArgs e)
8384
var password = txtPassword.Password;
8485
if (password != string.Empty) { options.Password = password.ToCharArray(); }
8586

87+
// Disable UI during connection attempt
88+
SetConnectingState(true);
89+
8690
try
8791
{
8892
try
8993
{
90-
Cursor = Cursors.Wait;
91-
try { vncControl.Client.Connect(hostname, port, options); }
92-
finally { Cursor = Cursors.Arrow; }
94+
// Run the blocking Connect call on a background thread
95+
await Task.Run(() => vncControl.Client.Connect(hostname, port, options));
9396
}
9497
catch (Vnc.VncException ex)
9598
{
@@ -113,10 +116,27 @@ private void btnConnect_Click(object sender, RoutedEventArgs e)
113116
{
114117
Array.Clear(options.Password, 0, options.Password.Length);
115118
}
119+
120+
// Restore UI state
121+
SetConnectingState(false);
116122
}
117123
}
118124
}
119125

126+
private void SetConnectingState(bool connecting)
127+
{
128+
btnConnect.IsEnabled = !connecting;
129+
txtHostname.IsEnabled = !connecting;
130+
txtPort.IsEnabled = !connecting;
131+
txtPassword.IsEnabled = !connecting;
132+
Cursor = connecting ? Cursors.Wait : Cursors.Arrow;
133+
134+
if (connecting)
135+
{
136+
btnConnect.Content = "Connecting...";
137+
}
138+
}
139+
120140
private void vncControl_Connected(object sender, EventArgs e)
121141
{
122142
btnConnect.Content = "Close";

RemoteViewing.WPF/Properties/AssemblyInfo.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3131
using System;
3232
using System.Reflection;
3333
using System.Runtime.InteropServices;
34+
using System.Windows;
35+
using System.Windows.Markup;
3436

3537
[assembly: AssemblyCopyright("Copyright © 2013, 2016, 2025 James F. Bellinger <http://software.seekye.com/remoteviewing>")]
3638
[assembly: AssemblyDescription("C# VNC client/server for WPF")]
3739
[assembly: AssemblyProduct("RemoteViewing")]
3840
[assembly: AssemblyTitle("RemoteViewing.WPF")]
39-
[assembly: AssemblyFileVersion("1.0.1.0")]
40-
[assembly: AssemblyVersion("1.0.1.0")]
41+
[assembly: AssemblyFileVersion("1.2.0.0")]
42+
[assembly: AssemblyVersion("1.2.0.0")]
4143
[assembly: CLSCompliant(true)]
4244
[assembly: ComVisible(false)]
4345
[assembly: Guid("5d389f2a-c1e5-47a2-b6c8-f87260e96e30")]
46+
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
47+
[assembly: XmlnsPrefix("https://github.com/lemutec/RemoteViewing", "vnc")]
48+
[assembly: XmlnsDefinition("https://github.com/lemutec/RemoteViewing", "RemoteViewing.WPF")]

RemoteViewing.WPF/Throw.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

RemoteViewing.Windows.Forms.Example/MainForm.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3030

3131
using System;
3232
using System.Net.Sockets;
33+
using System.Threading.Tasks;
3334
using System.Windows.Forms;
3435

3536
namespace RemoteViewing.Windows.Forms.Example;
@@ -42,7 +43,7 @@ public MainForm()
4243
UpdateTitle();
4344
}
4445

45-
private void btnConnect_Click(object sender, EventArgs e)
46+
private async void btnConnect_Click(object sender, EventArgs e)
4647
{
4748
if (vncControl.Client.IsConnected)
4849
{
@@ -68,13 +69,15 @@ private void btnConnect_Click(object sender, EventArgs e)
6869
var options = new Vnc.VncClientConnectOptions();
6970
if (txtPassword.Text != string.Empty) { options.Password = txtPassword.Text.ToCharArray(); }
7071

72+
// Disable UI during connection attempt
73+
SetConnectingState(true);
74+
7175
try
7276
{
7377
try
7478
{
75-
Cursor = Cursors.WaitCursor;
76-
try { vncControl.Client.Connect(hostname, port, options); }
77-
finally { Cursor = Cursors.Default; }
79+
// Run the blocking Connect call on a background thread
80+
await Task.Run(() => vncControl.Client.Connect(hostname, port, options));
7881
}
7982
catch (Vnc.VncException ex)
8083
{
@@ -98,10 +101,27 @@ private void btnConnect_Click(object sender, EventArgs e)
98101
{
99102
Array.Clear(options.Password, 0, options.Password.Length);
100103
}
104+
105+
// Restore UI state
106+
SetConnectingState(false);
101107
}
102108
}
103109
}
104110

111+
private void SetConnectingState(bool connecting)
112+
{
113+
btnConnect.Enabled = !connecting;
114+
txtHostname.Enabled = !connecting;
115+
txtPort.Enabled = !connecting;
116+
txtPassword.Enabled = !connecting;
117+
Cursor = connecting ? Cursors.WaitCursor : Cursors.Default;
118+
119+
if (connecting)
120+
{
121+
btnConnect.Text = "Connecting...";
122+
}
123+
}
124+
105125
private void vncControl_Connected(object sender, EventArgs e)
106126
{
107127
btnConnect.Text = "Close";

RemoteViewing.Windows.Forms/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3636
[assembly: AssemblyDescription("C# VNC client/server")]
3737
[assembly: AssemblyProduct("RemoteViewing")]
3838
[assembly: AssemblyTitle("RemoteViewing.Windows.Forms")]
39-
[assembly: AssemblyFileVersion("1.0.1.0")]
40-
[assembly: AssemblyVersion("1.0.1.0")]
39+
[assembly: AssemblyFileVersion("1.2.0.0")]
40+
[assembly: AssemblyVersion("1.2.0.0")]
4141
[assembly: CLSCompliant(true)]
4242
[assembly: ComVisible(false)]
4343
[assembly: Guid("4c279e1f-d4ac-46d1-b5c7-e76169d85d29")]

RemoteViewing/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
3737
[assembly: AssemblyDescription("C# VNC client/server")]
3838
[assembly: AssemblyProduct("RemoteViewing")]
3939
[assembly: AssemblyTitle("RemoteViewing")]
40-
[assembly: AssemblyFileVersion("1.1.0.0")]
41-
[assembly: AssemblyVersion("1.1.0.0")]
40+
[assembly: AssemblyFileVersion("1.2.0.0")]
41+
[assembly: AssemblyVersion("1.2.0.0")]
4242
[assembly: CLSCompliant(true)]
4343
[assembly: ComVisible(false)]
4444
[assembly: Guid("bd19082b-d2a8-46fd-b50f-a7fe0379ed3c")]

0 commit comments

Comments
 (0)