private async void buttonDirect_ClickAsync(object sender, EventArgs e) { int ov1 = Helper.ShowOverlay(this); FormDirectConnect formDirectConnect = new FormDirectConnect(); if (formDirectConnect.ShowDialog() == DialogResult.OK) { string ipToConnect = formDirectConnect.IP; int ov2 = Helper.ShowOverlay(this); FormLoading formLoading = new FormLoading("Connecting to " + ipToConnect, 20); formLoading.Show(); // Try to connect var result = await Task.Run(() => Connection.GetServiceOnIP(ipToConnect)); formLoading.Close(); Helper.HideOverlay(ov2); if (result != null) //Connected { bool pass = (result["Password"].CompareTo("True") == 0) ? true : false; string groupName = result["GroupName"]; Connect(ipToConnect, groupName, pass); } else { int ov3 = Helper.ShowOverlay(this); FormAlert formAlert = new FormAlert("Error", "Can't establish connection to the server. Check provided IP", true); formAlert.ShowDialog(); Helper.HideOverlay(ov3); } } Helper.HideOverlay(ov1); }
private void ShowLoadingFormMenuItem_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) { FormLoading loader = new FormLoading(); loader.StartPosition = FormStartPosition.CenterScreen; loader.Show(); // This is a pretty important part because on the FromCurrentSynchronizationContext() call you can get Exception if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); } var context = TaskScheduler.FromCurrentSynchronizationContext(); var task = new Task(() => { Excel.Application app = Globals.ThisAddIn.Application; app.Interactive = false; for (int i = 0; i < 500; i++) { app.StatusBar = string.Format("Iteration {0}", i); Thread.Sleep(5); } // This code is used to call Close() method Thread safe otherwise you get Cross-thread operation not valid Exception ThreadSafeHelper.InvokeControlMethodThreadSafe(loader, () => { loader.Close(); }); app.StatusBar = "Ready"; app.Interactive = true; }); task.Start(); }