0

我的服务代码位于OnStart()throws Exception(I) 中并且服务已停止。我不知道为什么会有前任。抛出?..这是我的代码:

public Service1()
{
    InitializeComponent();
}

Thread thread;

protected override void OnStart(string[] args)
{
    thread = new Thread(delegate()
    {
        string path = @"D:\levani\FolderListenerTest\ListenedFolder";
        FileSystemWatcher listener;
        listener = new FileSystemWatcher(path);
        listener.Created += new FileSystemEventHandler(listener_Created);
        listener.EnableRaisingEvents = true;
    });
    thread.Start();
}

public void listener_Created(object sender, FileSystemEventArgs e)
{
    File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
}

protected override void OnStop()
{
    thread.Abort();
}

日志

Log Name:      Application
Source:        .NET Runtime
Date:          6/11/2012 5:33:27 PM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      Levan-PC
Description:
Application: FolderListenerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
   at System.IO.__Error.WinIOError(Int32, System.String)
   at System.IO.File.InternalCopy(System.String, System.String, Boolean)
   at System.IO.File.Copy(System.String, System.String)
   at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
   at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name=".NET Runtime" />
    <EventID Qualifiers="0">1026</EventID>
    <Level>2</Level>
    <Task>0</Task>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime="2012-06-11T14:33:27.000000000Z" />
    <EventRecordID>18314</EventRecordID>
    <Channel>Application</Channel>
    <Computer>Levan-PC</Computer>
    <Security />
  </System>
  <EventData>
    <Data>Application: FolderListenerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
   at System.IO.__Error.WinIOError(Int32, System.String)
   at System.IO.File.InternalCopy(System.String, System.String, Boolean)
   at System.IO.File.Copy(System.String, System.String)
   at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
   at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
   at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
</Data>
  </EventData>
</Event>
4

4 回答 4

3

这可能是多种原因。请参阅File.Copy()文档,尤其是记录所有可能引发的异常的异常部分。

您需要包装您的 File.Copy() 并捕获任何异常,以便您可以做出适当的反应:

public void listener_Created(object sender, FileSystemEventArgs e)
{
    try
    {
        File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
    }
    catch {FileNotFoundException e)
    { 
        //do something if file isn't there
    }
    catch {UnauthorizedAccessException e)
    { 
        //do something if invalid permissions
    }

    //etc 
}
于 2012-06-11T13:50:46.997 回答
1

如果文件已经存在,额外的参数 true inFile.Copy将覆盖文件。我认为错误是文件已经存在。

File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name,true);

将代码放入try..catch block并捕获IOException异常。您可以登录文件以进行进一步调试。

WinIOError当文件名、目录名或卷标语法不正确时,我们会收到错误(当我们进入调用堆栈时)。所以只需检查正确的路径和文件名。

于 2012-06-11T13:49:01.913 回答
0

如果您创建一个新线程,您需要确保处理该线程上抛出的所有异常。 在由 Thread.Start() 创建的线程上发生的任何未处理的异常都会导致您的应用程序终止。

具体来说,构造函数FileSystemWatcher(string path)File.Copy(string sourceFileName, string destFileName)会引发一些您在当前代码中未处理的异常。这两个都在一个单独的线程上调用。由于文件已经存在,您很可能会收到 IOException(对同一文件的多次更改将导致您的代码尝试多次复制它,从而导致第一次之后的任何副本发生冲突)。

您可能应该更新您的 File.Copy 调用以使用File.Copy(string sourceFileName, string destFileName, bool overwrite)并将您的函数包装listener_Created在一个 try/catch 块中,该块会处理异常(而不是重新抛出它)。

于 2012-06-11T13:56:43.833 回答
0

我不知道为什么,但是在我用try {} catch {}包围我的代码之后,它工作得很好,你知道吗?这是代码:

public Service1()
        {
            InitializeComponent();
        }

        Thread thread;

        protected override void OnStart(string[] args)
        {
            try
            {
                thread = new Thread(delegate()
                {
                    string path = @"D:\levani\FolderListenerTest\ListenedFolder";
                    FileSystemWatcher listener; listener = new FileSystemWatcher(path);
                    listener.Created += new FileSystemEventHandler(listener_Created);
                    listener.EnableRaisingEvents = true;
                });
                thread.Start();
            }
            catch (Exception ex)
            {
                File.WriteAllText(@"D:\levani\bussite.txt", "thread: " + ex.ToString());
            }
        }

        public void listener_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
            }
            catch (Exception ex)
            {
                File.WriteAllText(@"D:\levani\bussite.txt", "File copy ex: " + ex.ToString());
            }
        }

        protected override void OnStop()
        {
            thread.Abort();
        }
于 2012-06-11T13:57:56.047 回答