异步似乎正在运行任务,直到到达Await标签为止

时间:2019-04-26 17:23:53

标签: c# async-await task nullreferenceexception aggregateexception

运行代码时,我正在为某些Quest(关键基础结构监视设备)使用备份管理器,它似乎正在运行我的任务,因为在调用.WhenAll之前将其添加到任务列表中。然后在调用.WhenAll时,我得到null ref异常和聚合异常。我使用的是telnet nuget,您可以假设telnet可以正常工作,并且可以同时运行它

我是C#中异步/等待的新手。香港专业教育学院试图摆脱telnet部分,只是运行HTTP,反之亦然。我已经删除了异步方法(runBackupAsync)并直接运行到异步类(BackupSingleBoxAsync)不变

(BackupSingleBoxAsync)

    private Quest QuestBox = new Quest();

    public BackupSingleBoxAsync(Quest q)
    {
        this.QuestBox.IP = q.IP;
        this.QuestBox.User = q.User;
        this.QuestBox.Password = q.Password;
        this.QuestBox.Type = q.Type;

    }

    public async Task<Quest> StartAsync()
    {
        Quest tempQuest = new Quest();
        while (QuestBox.User != null && QuestBox.Password != null)
        {
            if (QuestBox.Type.ToLower().Contains("mini"))
            {
                HttpDownload getRes = new HttpDownload(QuestBox);
                tempQuest = await getRes.Start();
            }
            else
            {
                Telnet getRes = new Telnet(QuestBox);
                tempQuest = await getRes.Start();
            }

        }
        this.QuestBox.Results = tempQuest.Results;

        return QuestBox;
    }

(备份控制器)

        GetConfigs config = new GetConfigs(QuestMasterList);
        List<Quest> questAterDownload = config.GetAll().Result;

(获取配置)

    public List<Quest> QuestMasterList = new List<Quest>();

    public GetConfigs(List<Quest> list)
    {
        this.QuestMasterList.AddRange(list);
    }

    public async Task<List<Quest>> GetAll()
    {
        List<Task<Quest>> task_list = new List<Task<Quest>>();
        foreach (var quest in QuestMasterList)
        {
            task_list.Add(runBackupAsync(quest));
        }

        await Task.WhenAll(task_list);

        List<Quest> tempQuests = new List<Quest>();

        foreach (var tsk_rslt in task_list)
        {
            tempQuests.Add(tsk_rslt.Result);
        }

        return tempQuests;
    }

    private async Task<Quest> runBackupAsync(Quest quest)
    {
        BackupSingleBoxAsync backup = new BackupSingleBoxAsync(quest);
        Quest res = backup.StartAsync().Result;
        return res;
    }

(任务)

    public string IP;
    public string User;
    public string Password;
    public string Type;
    public bool Success;
    public bool Diff;
    public List<string> Results = new List<string>();

我希望它返回任务框类型的列表,但会提供空ref异常和聚合异常

0 个答案:

没有答案