【问题标题】:I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?我有时在 System.Threading.Tasks.TaskCompletionSource<bool> 上不断收到异常,我该如何解决?
【发布时间】:2015-08-12 07:34:15
【问题描述】:

我有一个观察者方法,我从 fomr1 的构造函数调用:

FileSystemWatcher watcher;
        private void WatchDirectory()
        {
            watcher = new FileSystemWatcher();
            watcher.Path = userVideosDirectory;
            watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
            watcher.Filter = "*.mp4";
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.EnableRaisingEvents = true;
        } 

然后是 Onchanged 事件:

private void OnChanged(object source, FileSystemEventArgs e)
        {

                var info = new FileInfo(e.FullPath);
                fileforupload = info.FullName;
                if (IsFileLocked(info) == false)
                {
                    sy.SetResult(true);
                    watcher.EnableRaisingEvents = false;
                    watcher.Dispose();
                }
        }

这是 IsFileLocked 方法:

protected virtual bool IsFileLocked(FileInfo file)
        {
            FileStream stream = null;
            try
            {
                stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

我在这个方法中使用它:

public string SendResponse(HttpListenerRequest request)
        {            
                    sy = new TaskCompletionSource<bool>();
                    WatchDirectory();
                    sy.Task.Wait();
                    Youtube_Uploader youtubeupload = new Youtube_Uploader(fileforupload);
                    return false;
        }

异常是在OnChanged事件就行了:

sy.SetResult(true);

sy 是:

TaskCompletionSource<bool> sy;

在我做的 OnChanged 方法中:

watcher.EnableRaisingEvents = false;
watcher.Dispose();

但有时仍然会出现异常。 例外是:

An attempt was made to transition a task to a final state when it had already completed

System.InvalidOperationException was unhandled
  _HResult=-2146233079
  _message=An attempt was made to transition a task to a final state when it had already completed.
  HResult=-2146233079
  IsTransient=false
  Message=An attempt was made to transition a task to a final state when it had already completed.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.TaskCompletionSource`1.SetResult(TResult result)
       at Automatic_Record.Form1.OnChanged(Object source, FileSystemEventArgs e) in d:\C-Sharp\Automatic_Record\Automatic_Record\Automatic_Record\Form1.cs:line 197
       at System.IO.FileSystemWatcher.OnChanged(FileSystemEventArgs e)
       at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
       at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  InnerException: 

【问题讨论】:

  • sy.SetResult(true); 前面加一个Debug.WriteLine("setting result"); 会打印多次吗?

标签: c# .net winforms


【解决方案1】:

发生这种情况是因为 FileSystemWatcher 会针对任何单个更改触发多个 onChanged 事件。因此,在您禁用 EnableRaisingEvent 之前,观察者可能已经触发了多个 onChanged 事件。您需要在 onChanged 方法中加锁,或者使用 TaskCompletionSource.TrySetResult 而不是使用 TaskCompletionSource.SetResult。

有关 TrySetResult 的详细信息,请参阅:https://msdn.microsoft.com/en-us/library/dd449176(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2022-12-21
    • 2020-08-20
    相关资源
    最近更新 更多