C# 在System.Threading.Tasks.TaskCompletionSource<;上,我有时会遇到异常;布尔>;我怎样才能解决它?
我从fomr1的构造函数中调用了一个watcher方法:C# 在System.Threading.Tasks.TaskCompletionSource<;上,我有时会遇到异常;布尔>;我怎样才能解决它?,c#,.net,winforms,C#,.net,Winforms,我从fomr1的构造函数中调用了一个watcher方法: FileSystemWatcher watcher; private void WatchDirectory() { watcher = new FileSystemWatcher(); watcher.Path = userVideosDirectory; watcher.NotifyFilter = NotifyFilters.La
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;
}
sy是:
TaskCompletionSource<bool> sy;
但有时还是会有例外。
例外情况是:
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:
发生这种情况是因为FileSystemWatcher为任何单个更改触发多个onChanged事件。所以,在禁用EnableRaisingEventWatcher之前,它可能会触发多个onChanged事件。您需要在onChanged方法中设置锁定,或者使用TaskCompletionSource.TrySetResult代替TaskCompletionSource.SetResult 有关TrySetResult的详细信息,请参见此链接:放置一个
Debug.WriteLine(“设置结果”)代码>在sy.SetResult之前(true)代码>是否打印多次?
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: