void progressCrowdCount() { form1.DisableBtn("count"); int count = 0; this.Show(); Thread bgThread = new Thread( new ThreadStart(() => { count = crowdCount(); this.BeginInvoke( new Action(() => { PictureViewer pictureViewer = new PictureViewer("result", bitmap); pictureViewer.SetBitmap(bitmap); pictureViewer.setResult(result); pictureViewer.SetResultCount(count); pictureViewer.Show(); this.Dispose(); } )); form1.setResult("Crowd Count: " + count.ToString()); form1.EnableBtn("count"); } )); try { bgThread.Start(); }catch (Exception e) { Console.WriteLine(e.StackTrace); MessageBox.Show("Oops, Something went wrong, Please try again", "Error"); } }