Skip to content

Instantly share code, notes, and snippets.

@k4m4r82
Created February 9, 2016 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4m4r82/63391d8f511049303b20 to your computer and use it in GitHub Desktop.
Save k4m4r82/63391d8f511049303b20 to your computer and use it in GitHub Desktop.
using Microsoft.Owin.Hosting;
namespace DemoSignalR.WinFormServer
{
public partial class FrmServer : Form
{
private IDisposable signalR { get; set; }
private const string SERVER_URL = "http://192.168.56.1:8282";
public FrmServer()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
WriteToConsole("Starting server...");
btnStart.Enabled = false;
Task.Run(() => StartServer());
}
private void btnStop_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
/// Starts the server and checks for error thrown when another server is already
/// running. This method is called asynchronously from Button_Start.
/// </summary>
private void StartServer()
{
try
{
signalR = WebApp.Start(SERVER_URL);
}
catch (TargetInvocationException)
{
WriteToConsole("Server failed to start. A server is already running on " + SERVER_URL);
//Re-enable button to let user try to start server again
this.Invoke((Action)(() => btnStart.Enabled = true));
return;
}
this.Invoke((Action)(() => btnStop.Enabled = true));
WriteToConsole("Server started at " + SERVER_URL);
}
/// <summary>
/// This method adds a line to the RichTextBoxConsole control, using Invoke if used
/// from a SignalR hub thread rather than the UI thread.
/// </summary>
/// <param name="message"></param>
internal void WriteToConsole(String message)
{
if (lstConsole.InvokeRequired)
{
this.Invoke((Action)(() =>
WriteToConsole(message)
));
return;
}
lstConsole.Items.Add(message);
}
private void FrmServer_FormClosing(object sender, FormClosingEventArgs e)
{
if (signalR != null)
{
signalR.Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment