예제 #1
0
        public void SwitchController(MainViewType newViewType, bool visible = true)
        {
            if (CurrentViewType == newViewType)
            {
                return;
            }

            Log.InfoFormat("**** BEGIN SwitchController(): switch from {0} to {1} **** ", CurrentViewType, newViewType);

            using (new WaitCursor())
            {
                Items.Clear();
                if (visible)
                {
                    MainGridView.Columns.Clear();

                    LastSortOrder.Clear();

                    if (CurrentViewType != MainViewType.Invalid)
                    {
                        KnownViews[CurrentViewType].Button.Background = UnselectedBackgroundColor;
                        KnownViews[CurrentViewType].Button.Foreground = UnselectedForegroundColor;
                    }
                }


                // cleanup
                CurrentController = KnownViews[newViewType].Controller;

                // create columns
                DateTime startTime = DateTime.Now;
                try
                {
                    CurrentController.Refresh(Items);
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("Exception caught while refreshing {0}", CurrentController), e);
                }
                Log.InfoFormat("Total time for CurrentController.Refresh: {0}", DateTime.Now - startTime);
                Log.InfoFormat("Total items count: {0}", Items.Count);

                FindThisText.Text = "";
                if (visible)
                {
                    MainListView.ItemsSource = Items;   //your query result
                    foreach (DataObjectColumn oc in CurrentController.Columns)
                    {
                        GridViewColumn column = new GridViewColumn();
                        column.Header = oc.DisplayName;
                        column.DisplayMemberBinding = new Binding(oc.BindingName);
                        column.Width = System.Double.NaN;
                        MainGridView.Columns.Add(column);
                    }
                    CurrentController.MainListView   = MainListView;
                    MainListView.ContextMenu         = CurrentController.ContextMenu;
                    MainListView.ContextMenuOpening += MainListView_ContextMenuOpening;

                    UpdateDefaultStatusBar();

                    TbControlStart.Text    = CurrentController.ControlStartDescription;
                    TbControlStop.Text     = CurrentController.ControlStopDescription;
                    TbControlRestart.Text  = CurrentController.ControlRestartDescription;
                    TbControlPause.Text    = CurrentController.ControlPauseDescription;
                    TbControlContinue.Text = CurrentController.ControlContinueDescription;

                    UpdateTitle();

                    CreateInitialSort();
                    KnownViews[newViewType].Button.Background = SelectedBackgroundColor;
                    KnownViews[newViewType].Button.Foreground = SelectedForegroundColor;

                    if (FirstDisplay)
                    {
                        FindThisText.Text = App.Settings.LastSearchText;


                        FirstDisplay = false;
                    }
                }
                CurrentViewType = newViewType;

                App.Settings.LastViewType = CurrentViewType.ToString();

                if ((InitialSSR != null) && (InitialServiceNames != null))
                {
                    if (newViewType == MainViewType.Services)
                    {
                        services.ServicesDataController sdc = CurrentController as services.ServicesDataController;
                        sdc.PerformExplicitRequest(InitialSSR, InitialServiceNames,
                                                   pserv4.Properties.Resources.IDS_PERFORMING_ACTION);
                        Close();
                    }
                    else
                    {
                        InitialSSR          = null;
                        InitialServiceNames = null;
                    }
                }
            }

            Log.Info("**** END SwitchController() ****");
        }
예제 #2
0
        public override void DoWork()
        {
            ACCESS_MASK ServiceAccessMask = SSR.GetServiceAccessMask() | ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.SERVICE_QUERY_STATUS;

            ServicesDataController sdc = MainWindow.CurrentController as ServicesDataController;

            using (NativeSCManager scm = new NativeSCManager(sdc.MachineName))
            {
                int serviceIndex = 0;
                foreach (ServiceDataObject so in Services)
                {
                    ++serviceIndex;

                    try
                    {
                        SetOutputText(string.Format("Service {0}/{1}: {2} is initially in state {3}",
                                                    serviceIndex,
                                                    Services.Count,
                                                    so.DisplayName,
                                                    ServicesLocalisation.Localized(so.CurrentState)));

                        if (so.CurrentState == SC_RUNTIME_STATUS.SERVICE_STOPPED)
                        {
                            ServiceAccessMask &= ~(ACCESS_MASK.SERVICE_STOP);
                        }

                        using (NativeService ns = new NativeService(scm, so.InternalID, ServiceAccessMask))
                        {
                            bool requestedStatusChange = false;

                            Log.InfoFormat("BEGIN backgroundWorker1_Process for {0}", ns.Description);
                            using (ServiceStatus ss = new ServiceStatus(ns))
                            {
                                for (int i = 0; i < 100; ++i)
                                {
                                    if (Worker.CancellationPending)
                                    {
                                        break;
                                    }

                                    if (!ss.Refresh())
                                    {
                                        break;
                                    }

                                    SetOutputText(string.Format("Service {0}/{1}: {2} is now in state {3}",
                                                                serviceIndex,
                                                                Services.Count,
                                                                so.DisplayName,
                                                                ServicesLocalisation.Localized(ss.Status.CurrentState)));

                                    if (SSR.HasSuccess(ss.Status.CurrentState))
                                    {
                                        Log.Info("Reached target status, done...");
                                        break; // TODO: reached 100% of this service' status reqs.
                                    }

                                    // if we haven't asked the service to change its status yet, do so now.
                                    if (!requestedStatusChange)
                                    {
                                        requestedStatusChange = true;
                                        Log.InfoFormat("Ask {0} to issue its status request on {1}", SSR, ss);
                                        if (!SSR.Request(ss))
                                        {
                                            break;
                                        }
                                    }
                                    else if (SSR.HasFailed(ss.Status.CurrentState))
                                    {
                                        Log.Error("ERROR, target state is one of the failed ones :(");
                                        break;
                                    }
                                    Thread.Sleep(500);
                                }
                                so.UpdateFrom(ss.Status);
                                Log.Info("END backgroundWorker1_Process");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Exception caught in PerformServiceStateRequest", ex);
                    }
                    if (Worker.CancellationPending)
                    {
                        break;
                    }
                }
            }
        }