简体   繁体   English

如何在抛出异常中将我的文本显示为错误文本?

[英]how do i show my text as an error text in throw exception?

i wrote a window service project, i want to display an error message when some error occur, in OnStart() function, (with throw) then service isn't started. 我编写了一个窗口服务项目,我想在OnStart()函数中(带有throw)出现一些错误时显示一条错误消息,然后服务无法启动。

protected override void OnStart(string[] args)
{
    // do some code ...

    // when some error occur:
    throw new System.Exception("my error text!!!");
 }

it is ok, and service isn't started. 可以,并且服务未启动。 But 'my error text' isn't displayed!!! 但是不会显示“我的错误文字” !!! and the system shows own error text("Some services stop automatically if ...") 并且系统显示自己的错误文本(“如果...,某些服务会自动停止”)

how do i show my text as an error text for users, when he starts service and some error occur? 当用户开始服务并出现一些错误时,如何显示我的文本作为错误文本?

You could try to display an error message using the MessageBox function of the User32.dll : 您可以尝试使用User32.dllMessageBox函数显示错误消息:

private const int MB_SERVICE_NOTIFICATION = 0x00200000;
private const int MB_ICONERROR = 0x00000010;

[DllImport("User32", EntryPoint = "MessageBox")]
private extern static int MsgBox(int hwnd, string lpText, string lpCaption, uint uType); 

protected override void OnStart(string[] args)
{
    // do some code ...

    // when some error occurred:
    MsgBox(0, "my error text!!!", "Error", MB_SERVICE_NOTIFICATION | MB_ICONERROR);
    throw new ApplicationException();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM