第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我正在運行服務嗎

我正在運行服務嗎

慕田峪9158850 2019-12-27 10:00:55
我目前正在為可以在控制臺中運行的服務編寫一些引導程序代碼。從本質上講,它歸結為調用OnStart()方法,而不是使用ServiceBase來啟動和停止服務(因為如果未將其作為服務安裝,則它不會運行該應用程序,并且會使調試成為噩夢)?,F(xiàn)在,我正在使用Debugger.IsAttached確定是否應該使用ServiceBase.Run或[service] .OnStart,但是我知道這不是最好的主意,因為有時最終用戶希望在控制臺中運行該服務(以查看輸出等)。關于如何確定Windows服務控制器是否在控制臺中啟動“我”或用戶是否在控制臺中啟動“我”的任何想法?顯然,Environment.IsUserInteractive不是答案。我考慮過使用命令行參數(shù),但這似乎“骯臟”。我總是可以看到圍繞ServiceBase.Run的try-catch語句,但這似乎很臟。編輯:嘗試捕獲不起作用。我有一個解決方案:將其放置在這里,供所有其他感興趣的堆疊器使用:    public void Run()    {        if (Debugger.IsAttached || Environment.GetCommandLineArgs().Contains<string>("-console"))        {            RunAllServices();        }        else        {            try            {                string temp = Console.Title;                ServiceBase.Run((ServiceBase[])ComponentsToRun);            }            catch            {                RunAllServices();            }        }    } // void Run    private void RunAllServices()    {        foreach (ConsoleService component in ComponentsToRun)        {            component.Start();        }        WaitForCTRLC();        foreach (ConsoleService component in ComponentsToRun)        {            component.Stop();        }    }編輯:在StackOverflow上還有另一個問題,那里的人與Environment.CurrentDirectory是“ C:\ Windows \ System32”的問題看起來可能是答案。我今天要測試。
查看完整描述

3 回答

?
寶慕林4294392

TA貢獻2021條經(jīng)驗 獲得超8個贊

像Ash一樣,我將所有實際的處理代碼編寫在單獨的類庫程序集中,然后由Windows Service可執(zhí)行文件以及控制臺應用程序引用。


但是,有時候了解類庫是否在服務可執(zhí)行文件或控制臺應用程序的上下文中運行很有用。我這樣做的方法是反思托管應用程序的基類。(對VB表示抱歉,但是我認為可以很容易地對C#進行以下修飾):


Public Class ExecutionContext

    ''' <summary>

    ''' Gets a value indicating whether the application is a windows service.

    ''' </summary>

    ''' <value>

    ''' <c>true</c> if this instance is service; otherwise, <c>false</c>.

    ''' </value>

    Public Shared ReadOnly Property IsService() As Boolean

        Get

            ' Determining whether or not the host application is a service is

            ' an expensive operation (it uses reflection), so we cache the

            ' result of the first call to this method so that we don't have to

            ' recalculate it every call.


            ' If we have not already determined whether or not the application

            ' is running as a service...

            If IsNothing(_isService) Then


                ' Get details of the host assembly.

                Dim entryAssembly As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly


                ' Get the method that was called to enter the host assembly.

                Dim entryPoint As System.Reflection.MethodInfo = entryAssembly.EntryPoint


                ' If the base type of the host assembly inherits from the

                ' "ServiceBase" class, it must be a windows service. We store

                ' the result ready for the next caller of this method.

                _isService = (entryPoint.ReflectedType.BaseType.FullName = "System.ServiceProcess.ServiceBase")


            End If


            ' Return the cached result.

            Return CBool(_isService)

        End Get

    End Property


    Private Shared _isService As Nullable(Of Boolean) = Nothing

#End Region

End Class


查看完整回答
反對 回復 2019-12-27
?
江戶川亂折騰

TA貢獻1851條經(jīng)驗 獲得超5個贊

什么對我有用:


進行實際服務工作的類在單獨的線程中運行。

此線程從OnStart()方法內(nèi)部啟動,并從OnStop()停止。

服務和控制臺模式之間的決定取決于 Environment.UserInteractive

樣例代碼:


class MyService : ServiceBase

{

    private static void Main()

    {

        if (Environment.UserInteractive)

        {

            startWorkerThread();

            Console.WriteLine ("======  Press ENTER to stop threads  ======");

            Console.ReadLine();

            stopWorkerThread() ;

            Console.WriteLine ("======  Press ENTER to quit  ======");

            Console.ReadLine();

        }

        else

        {

            Run (this) ;

        }

    }


    protected override void OnStart(string[] args)

    {

        startWorkerThread();

    }


    protected override void OnStop()

    {

        stopWorkerThread() ;

    }

}


查看完整回答
反對 回復 2019-12-27
  • 3 回答
  • 0 關注
  • 588 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號