0

在运行代码时,我正在为一些 Quest(关键基础设施监控设备)开发备份管理器,它似乎正在运行我的任务,因为它在调用 .WhenAll 之前将它们添加到任务列表中。然后当调用 .WhenAll 我得到空引用异常和聚合异常。我正在使用 telnet nuget,您可以假设 telnet 可以独立运行并且同步运行

我是 C# 中 Async/Await 的新手。我试过摆脱 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>();

我希望它返回一个任务框类型列表,但它提供空引用异常和聚合异常

4

0 回答 0