如何在async&中释放死锁线程等待

时间:2016-02-15 07:21:50

标签: c# multithreading asynchronous

我正在使用async&等待用于下载项目VSO API的方法,但有时候线程永远不会返回并且它会持续等待流数据。那么我需要做什么以防万一。我不知道下载的大概时间。如果我们可以做任何等待超过24小时的线程暂停。

请在下面的代码中提出建议 -

public static async Task<string> DownloadProject(string Project, string sourceUrl, string targetdir, string BackPlanName, int callbyservice)
{
    FileStream fileStream = null;
    DateTime starttime = DateTime.Now;
    string str = "";
    ILogger ilogger = new Logger();
    ConfigurationSettings objXConfiguration = new ConfigurationSettings();
    System.Net.ServicePointManager.Expect100Continue = false;
    try
    {
        var username = objConfigurationSettings.UserName;
        var password = objConfigurationSettings.Password;
        using (HttpClient client = new HttpClient())
        {

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;


            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/zip"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", username, password))));
            client.DefaultRequestHeaders.Add("Connection", new string[] { "Keep-Alive" });
            //=============================================================================================================
            string FilePath = targetdir + "\\" + BackPlanName;
            //  string CurrentDate = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second;
            str = Project + ".zip";

            if (!Directory.Exists(FilePath))
            {
                Directory.CreateDirectory(FilePath);
            }
            fileStream = new FileStream(FilePath + "\\" + str, FileMode.Create, FileAccess.Write);



            System.Net.ServicePointManager.Expect100Continue = false;

            var stream = await client.GetStreamAsync(sourceUrl).ConfigureAwait(continueOnCapturedContext: false);
            await stream.CopyToAsync(fileStream).ConfigureAwait(continueOnCapturedContext: false);



            //===========================================================================
            return FilePath + "\\" + str;
        }
    }
    catch (Exception e)
    {


       throw e;
    }

    finally {

    }
}

1 个答案:

答案 0 :(得分:0)

仅使用取消令牌

public static async Task DownloadProject(string Project,string sourceUrl,string targetdir,string BackPlanName,int callbyservice)         {             CancellationTokenSource cts = new CancellationTokenSource();             cts.CancelAfter(1000 * 60 * 60 * 23);             FileStream fileStream = null;             DateTime starttime = DateTime.Now;             string str =“”;             ILogger ilogger = new Logger();             ConfigurationSettings objXConfiguration = new ConfigurationSettings();             System.Net.ServicePointManager.Expect100Continue = false;             尝试             {                 var username = objConfigurationSettings.UserName;                 var password = objConfigurationSettings.Password;                 使用(HttpClient客户端=新的HttpClient())                 {

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;


                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/zip"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", username, password))));
                client.DefaultRequestHeaders.Add("Connection", new string[] { "Keep-Alive" });
                //=============================================================================================================
                string FilePath = targetdir + "\\" + BackPlanName;
                //  string CurrentDate = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second;
                str = Project + ".zip";

                if (!Directory.Exists(FilePath))
                {
                    Directory.CreateDirectory(FilePath);
                }
                fileStream = new FileStream(FilePath + "\\" + str, FileMode.Create, FileAccess.Write);



                System.Net.ServicePointManager.Expect100Continue = false;


                var stream = await client.GetStreamAsync(sourceUrl).ConfigureAwait(continueOnCapturedContext: false);
                await stream.CopyToAsync(fileStream,81920,cts.Token).ConfigureAwait(continueOnCapturedContext: true); 


                string CalculateSize = CalculateFileSize(FilePath + "\\" + str);
                object _lock = new object();
                IXMLData ixmlBackup = new XMLData();

                EventWaitHandle waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset);
                waitHandle.WaitOne();
                ixmlBackup.CreateBackupResultXML(UtlityHelper.GetFilePath(Settings.Default.BackupResult), BackPlanName, starttime.ToString(), DateTime.Now.ToString(), "Success", CalculateSize + " files copied");
                waitHandle.Set();
                // waitHandle.Dispose();

                //==================Email when download compelte in window service===========================//
                if (callbyservice == 1)
                {
                    objXConfiguration = ixmlBackup.GetSettings("application");
                    ObservableCollection<BackupPlan> BackupPlanCollection = ixmlBackup.GetBackupPlan();
                    var CurrentPlan = BackupPlanCollection.Where(r => r.BackupPlanName == BackPlanName).FirstOrDefault();
                    DeleteFile(CurrentPlan.KeepBackupFileCount, CurrentPlan.TargetLocation + CurrentPlan.BackupPlanName, objXConfiguration.EmailAlertLevel, objConfigurationSettings.Service, ilogger);
                    if (objXConfiguration.EmailAlertLevel == "1" || objXConfiguration.EmailAlertLevel == "2")
                    {
                        UtlityHelper.SendEmail(DateTime.Now.ToString() + ":" + objConfigurationSettings.Service + ":The download for the backup profile "+BackPlanName+" completed.", 1);
                    }
                    ilogger.WriteBackLog(DateTime.Now.ToString() + ":" + objConfigurationSettings.Service + ":The download for the backup profile " + BackPlanName + " completed.");
                }
                //===========================================================================
                return FilePath + "\\" + str;
            }
        }
        catch (Exception e)
        {
            IXMLData ixmlBackup = new XMLData();
            ixmlBackup.CreateBackupResultXML(UtlityHelper.GetFilePath(Settings.Default.BackupResult), BackPlanName, starttime.ToString(), DateTime.Now.ToString(), "Fail", e.Message);
            if (callbyservice == 1)
            {
                ilogger.WriteBackupException(e);
                objXConfiguration = ixmlBackup.GetSettings("application");
                if (objXConfiguration.EmailAlertLevel == "0" || objXConfiguration.EmailAlertLevel == "2")
                {
                    UtlityHelper.SendEmail(DateTime.Now.ToString() + ":" + objConfigurationSettings.Service + ": Exception:" + BackPlanName + ":" + e.Message, 1);
                }
            }
            else
            {
                throw e;
            }
            return null;
        }

        finally {
            fileStream.Flush();
            fileStream.Dispose();

        }
    }