【发布时间】:2012-08-01 07:23:57
【问题描述】:
我正在为一个网站编写视频转换工具。
接下来是逻辑
- 用户上传文件
- 系统为用户转换(当然用户在转换时不应等待)
我有一个代码:
public async void AddVideo(HotelVideosViewModel model)
{
var sourceFile = FileHandler.SaveFile(model.VideoFile);
var fullFilePath = HttpContext.Current.Server.MapPath(sourceFile);
await Task.Factory.StartNew(() => video.Convert(fullFilePath));
model.FileLocation = sourceFile;
model.IsConverted = true;
this.Add<HotelVideos>(Mapper.Map<HotelVideosViewModel, HotelVideos>(model));
}
这个开始编码:
await Task.Factory.StartNew(() => video.Convert(fullFilePath));
但是这之后的代码永远不会执行......有人有解决方案吗?
PS:在 video.Conver(...) 下有类似:
public bool Convert(string fileLocation)
{
// do staff
}
--- 更新 ---
刚刚得到了有趣的东西。如果我要将video.Convert(fullFilePath) 交换为 Thread.Sleep(2000) 一切都会开始工作。所以我认为问题出在第二种方法中。
所以我正在添加下一个文件代码(现在草稿):
using Softpae.Media;
/// <summary>
/// TODO: Update summary.
/// </summary>
public class VideoConvertManager
{
private readonly Job2Convert jobConverter = new Job2Convert();
private readonly MediaServer mediaServer = new MediaServer();
public bool Convert(string fileLocation)
{
this.jobConverter.pszSrcFile = fileLocation;
this.jobConverter.pszDstFile = fileLocation + ".mp4";
this.jobConverter.pszDstFormat = "mp4";
this.jobConverter.pszAudioCodec = null;
this.jobConverter.pszVideoCodec = "h264";
if (this.mediaServer.ConvertFile(this.jobConverter))
{
FileHandler.DeleteFile(fileLocation);
return true;
};
FileHandler.DeleteFile(fileLocation);
return false;
}
}
【问题讨论】:
-
PPS。这是我第一次使用 async/await :)
-
await 在 convert 完成之前不会返回,convert 是否已经完成?
-
致Frobzig:当然,检查。对 cshemby:是的,我做到了。
-
什么叫 AddVideo?除了需要 void 返回类型的处理程序之外,您通常希望避免使用“async void”。
标签: c# asp.net-mvc-3 asynchronous async-await