首页 > 解决方案 > 为什么我不能在休息时调试;行以及为什么列表包含空项目?

问题描述

我创建的一个新课程:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Extract
{
    class Satellite
    {
        private List<string> satelliteUrls = new List<string>();
        private string mainUrl = "https://some.com/";
        private string[] statements;

        public async Task DownloadSatelliteAsync()
        {
            using (var client = new WebClient())
            {
                client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");

                client.DownloadFileCompleted += (s, e) =>
                {
                    if(e.Error == null)
                    {
                        ExtractLinks();
                    }
                };

                await client.DownloadFileTaskAsync(new Uri(mainUrl), @"d:\Downloaded Images\Satellite\extractfile" + ".txt");
            }
        }

        private void ExtractLinks()
        {
            var file = File.ReadAllText(@"d:\Downloaded Images\images\extractfile" + ".txt");

            int idx = file.IndexOf("arrayImageTimes.push");
            int idx1 = file.IndexOf("</script>", idx);

            string results = file.Substring(idx, idx1 - idx);
            statements = results.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < statements.Length; i++)
            {
                if (i == 10)
                {
                    break;
                }

                string time = statements[i].Split('\'')[1];

                satelliteUrls.Add("https://some.com=" + time);
            }
        }
    }
}

在 Form1 顶部:

Satellite sat;

在 Form1 的构造函数中:

sat = new Satellite();

还有一个按钮点击事件:

private async void btnStart_Click(object sender, EventArgs e)
        {
            lblStatus.Text = "Downloading...";
            await sat.DownloadSatelliteAsync();
        }

下载的很好。

问题出在卫星类中,我无法在中断处添加断点;循环中的行:

if (i == 10)
                    {
                        break;
                    }

我可以在 if 和 close } 上设置断点,但不能在 break 上

其次,当它结束提取链接时,我可以在循环结束后在 close } 的底部放置一个断点,但是我看不到列表项。这就是我所看到的:

有 16 个项目,但大小是 10?

大小应为 10,因为中断;当 i = 10 但为什么列表有 16 个项目?

列表

当我单击项目时:

列表

我删除了链接地址,但有 10 个项目,但还有 6 个空项目。这个空值是从哪里来的?以及为什么我不能在休息时添加断点;线 ?为什么当我将鼠标放在列表中时,我看到的是这种 rex x 而不是物品?

标签: c#winforms

解决方案


_items是存储列表中实体的内部数组。它的大小对应于列表的.Capacity属性。

_items不会通过添加项目来扩展 1:1。当它用完时,它通常会使可用容量增加一倍。源代码。默认容量是 4(根据来源),所以它会加倍到 8,然后到 16,这正是我们所看到的。

您不能暂停执行,break;因为i永远不会达到 10 的值。

至于红色的 X,旁边的文字说:

用户关闭隐式函数评估

您可以在此处找到解决方案。本质上:进入选项,并在“调试”下确保选中“启用属性评估和其他隐式函数调用”。


推荐阅读