void asyncConnectHandler(IAsyncResult ar) { if (InvokeRequired) { BeginInvoke(new AsyncCallback(asyncConnectHandler), new object[] { ar }); return; } PLC p = (PLC)ar.AsyncState; try { p.EndConnect(ar); connectStatusL.Text = "Connected asynchronously!"; } catch (InvalidCredentialException exc) { connectStatusL.Text = "Invalid password"; } catch (Exception exc) { connectStatusL.Text = "Error: " + exc.Message; } }