public void UpdateProjectList(Server server)
        {
            this.server = server;
#if SYNCRHONOUS
            List<Project> dataSource = new List<Project>();

            if (server != null)
            {
                IList<Project> projects = hudsonService.LoadProjects(server);
                foreach (Project project in projects)
                    dataSource.Add(project);
            }

            SetProjectsDataSource(dataSource);
#else
            // clear the view
            projectsGridControl.DataSource = null;

            if (server == null)
                return;

            // disable the window, change the cursor, update the status
            Cursor.Current = Cursors.WaitCursor;
            Enabled = false;
            string status = string.Format(HudsonTrayTrackerResources.LoadingProjects_FormatString, server.Url);
            Controller.SetStatus(status, true);

            // run the process in background
            Process process = new Process("Loading project " + server.Url);
            IList<Project> projects = null;
            process.DoWork += delegate
            {
                projects = HudsonService.LoadProjects(server);
            };
            process.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                string endStatus = "";

                if (e.Error == null)
                {
                    var dataSource = new List<Project>();
                    foreach (Project project in projects)
                        dataSource.Add(project);
                    SetProjectsDataSource(dataSource);
                }
                else
                {
                    endStatus = string.Format(HudsonTrayTrackerResources.FailedLoadingProjects_FormatString, server.Url);
                }

                // enable the window, change the cursor, update the status
                Enabled = true;
                Cursor.Current = Cursors.Default;
                Controller.SetStatus(endStatus, false);
            };
            BackgroundProcessExecutor.Execute(process);
#endif
        }
        public static void Execute(Process process)
        {
#if false
            if (Thread.CurrentThread.IsBackground)
                throw new Exception("Should not run in background!");
            int fgThreadId = Thread.CurrentThread.ManagedThreadId;
#endif

            if (logger.IsDebugEnabled)
                logger.Debug("Running process: " + process.Description);

            // run that process
            BackgroundWorker bgWorker = new BackgroundWorker();
            ErrorHolder errorHolder = new ErrorHolder();
            bgWorker.DoWork += delegate(object sender, DoWorkEventArgs e)
            {
#if DEBUG
                if (Thread.CurrentThread.IsBackground == false)
                    throw new Exception("Should run in background!");
#endif

#if DONT_CATCH_EXCEPTION
                process.Run(sender, e);
#else
                // here we catch exceptions because otherwise, Visual Studio breaks,
                // even though the BackgroundWorker itself catches the exceptions!
                try
                {
                    process.Run(sender, e);
                }
                catch (Exception error)
                {
                    errorHolder.Error = error;
                }
#endif
            };
            bgWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
#if false
                if (Thread.CurrentThread.IsBackground)
                    throw new Exception("Should not run in background!");
                if (Thread.CurrentThread.ManagedThreadId != fgThreadId)
                    throw new Exception("RunWorkerCompleted called in another thread");
#endif

                // replace the args with one containing the caught error (if any)
                e = new RunWorkerCompletedEventArgs(e.Result, errorHolder.Error, e.Cancelled);

                if (logger.IsDebugEnabled)
                    logger.Debug("Done running process: " + process.Description);
                if (e.Error != null)
                    LoggingHelper.LogError(logger, e.Error);

                process.OnCompleted(sender, e);
            };
            bgWorker.RunWorkerAsync();
        }
        public static void Execute(Process process)
        {
            #if false
            if (Thread.CurrentThread.IsBackground)
                throw new Exception("Should not run in background!");
            int fgThreadId = Thread.CurrentThread.ManagedThreadId;
            #endif

            if (logger.IsDebugEnabled)
                logger.Debug("Running process: " + process.Description);

            // run that process
            BackgroundWorker bgWorker = new BackgroundWorker();
            ErrorHolder errorHolder = new ErrorHolder();
            bgWorker.DoWork += delegate(object sender, DoWorkEventArgs e)
            {
            #if DEBUG
                if (Thread.CurrentThread.IsBackground == false)
                    throw new Exception("Should run in background!");
            #endif

            #if DONT_CATCH_EXCEPTION
                process.Run(sender, e);
            #else
                // here we catch exceptions because otherwise, Visual Studio breaks,
                // even though the BackgroundWorker itself catches the exceptions!
                try
                {
                    if (!BadCredentialsHandler.ServersWithFailedAuthorizationUrlList.Contains(process.Description))
                    {
                        process.Run(sender, e);
                    }
                }
                catch (System.Net.WebException webException)
                {
                    if (webException.Status == System.Net.WebExceptionStatus.ProtocolError)
                    {
                        var response = webException.Response as System.Net.HttpWebResponse;

                        if (response != null)
                        {
                            if ((int)response.StatusCode == 401)
                            {
                                lock (BadCredentialsHandler.ServersWithFailedAuthorizationUrlList)
                                {
                                    if (!BadCredentialsHandler.ServersWithFailedAuthorizationUrlList.Contains(process.Description))
                                    {
                                        BadCredentialsHandler.ServersWithFailedAuthorizationUrlList.Add(process.Description);
                                    }
                                }
                            }
                        }
                    }

                    errorHolder.Error = webException;
                }
                catch (Exception error)
                {
                    errorHolder.Error = error;
                }
            #endif
            };
            bgWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
            #if false
                if (Thread.CurrentThread.IsBackground)
                    throw new Exception("Should not run in background!");
                if (Thread.CurrentThread.ManagedThreadId != fgThreadId)
                    throw new Exception("RunWorkerCompleted called in another thread");
            #endif

                // replace the args with one containing the caught error (if any)
                e = new RunWorkerCompletedEventArgs(e.Result, errorHolder.Error, e.Cancelled);

                if (logger.IsDebugEnabled)
                    logger.Debug("Done running process: " + process.Description);
                if (e.Error != null)
                    LoggingHelper.LogError(logger, e.Error);

                process.OnCompleted(sender, e);
            };
            bgWorker.RunWorkerAsync();
        }
        public void UpdateProjectList(Server server)
        {
            this.server = server;
            #if SYNCRHONOUS
            List<Project> dataSource = new List<Project>();

            if (server != null)
            {
                IList<Project> projects = hudsonService.LoadProjects(server);
                foreach (Project project in projects)
                    dataSource.Add(project);
            }

            SetProjectsDataSource(dataSource);
            #else
            // clear the view
            projectsGridControl.DataSource = null;

            if (server == null || server.CredentialsOutOfDate)
                return;

            // disable the window, change the cursor, update the status
            Cursor.Current = Cursors.WaitCursor;
            Enabled = false;
            string status = string.Format(HudsonTrayTrackerResources.LoadingProjects_FormatString, server.Url);
            Controller.SetStatus(status, true);

            // run the process in background
            Process process = new Process("Loading project " + server.Url);
            IList<Project> projects = null;
            process.DoWork += delegate
            {
                projects = HudsonService.LoadProjects(server);
            };
            process.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                string endStatus = "";

                if (e.Error == null)
                {
                    var dataSource = new List<Project>();
                    foreach (Project project in projects)
                        dataSource.Add(project);
                    SetProjectsDataSource(dataSource);
                }
                else
                {
                    if (e.Error is System.Net.WebException)
                    {
                        var webException = (System.Net.WebException)e.Error;

                        if (webException.Status == System.Net.WebExceptionStatus.ProtocolError)
                        {
                            var response = webException.Response as System.Net.HttpWebResponse;

                            if (response != null)
                            {
                                if ((int)response.StatusCode == 401)
                                {
                                    if (!BadCredentialsHandler.ServersWithFailedAuthorization.Contains(server))
                                    {
                                        BadCredentialsHandler.ServersWithFailedAuthorization.Add(server);
                                    }
                                }
                            }
                        }
                    }
                    endStatus = string.Format(HudsonTrayTrackerResources.FailedLoadingProjects_FormatString, server.Url);
                }

                // enable the window, change the cursor, update the status
                Enabled = true;
                Cursor.Current = Cursors.Default;
                Controller.SetStatus(endStatus, false);
            };

            if(!BadCredentialsHandler.ServersWithFailedAuthorization.Contains(server))
            {
                BackgroundProcessExecutor.Execute(process);
            }
            #endif
        }