简体   繁体   中英

Part of the code is non-reachable after using c# await

I'm writing the video converting tool for one web site.

Logic is next

  • User upload the file
  • System conver it for user (of course user should not wait while converting)

I have a code:

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));
        }

This start encoding:

await Task.Factory.StartNew(() => video.Convert(fullFilePath));

But the code after this is never executed... Does somebody has a solution?

PS: under video.Conver(...) is have something like:

public bool Convert(string fileLocation)
{
// do staff
}

--- UPDATE ---

Just got the interesting thing. If i'm going to exchange video.Convert(fullFilePath) to Thread.Sleep(2000) everything start to work. So the issue is in Second method i think.

So i'm adding the next file code (draft now):

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;
        }
    }   

You need to make AddVideo return Task , and await its result before completing the ASP.NET request.

Update: DO NOT call Wait or other blocking methods on an asynchronous Task ! This causes deadlock.

You may find my async intro helpful.

PS async doesn't change the way HTTP works. You still only have one response per request. If you want your end user (eg, browser) to not block, then you'll have to implement some kind of queue system where the browser can start video conversion jobs and be notified of their result (eg, using SignalR). async won't just magically make this happen - it only works within the context of a single request/response pair.

添加ConfigureAwait(false)将阻止继续在请求线程上被强制执行

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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