【问题标题】:Singleton property not updating during "OnClicked" event在“OnClicked”事件期间未更新单例属性
【发布时间】:2021-05-19 16:03:28
【问题描述】:

我确定我记得这是一个线程问题,但我找不到答案。看起来应该很简单。我有以下代码:

    private void Dingle_Clicked(object sender, RoutedEventArgs e)
    {
        dynamic doc = ScraperBrowser.Document;
        string htmlText = doc.documentElement.InnerHtml;
        htmlText = htmlText.Replace("\r\n", " ");

        Regex targetStart = new Regex(this works just fine);
        MatchCollection target = targetStart.Matches(htmlText);

        string priceData = target[0].Value; 

        foreach (StorePriceData spData in Lists.Singleton.MedicineList[medIndex].Prices)
        {
            Regex rx = new Regex(spData.StoreName + @".+?(\$\d+\.\d+)");
            MatchCollection matches = rx.Matches(priceData);
            if (matches.Count > 0)
            {
                if (matches[0].Groups.Count > 0)
                {
                    spData.MedicinePrice = matches[0].Groups[1].Value;
                }
            }
        }

        string cookie = Application.GetCookie(new Uri("https://www.goodrx.com"));
        ++medIndex;
        ScraperBrowser.Navigate(Lists.Singleton.MedicineList[medIndex].GoodRxUrlString);
    }

我遇到的问题是 spData.MedicinePrice 采用该值,但未更新单例“MedicineList”中的值。如何更新该值?

单例代码:

public class Lists
{
    private static Lists _singleton;
    public static Lists Singleton
    {
        get
        {
            if (_singleton == null) _singleton = new Lists(); return _singleton;
        }
    }

    public List<MedicineInfo> MedicineList {
        get
        {
            return new List<MedicineInfo>()
            {
                new MedicineInfo() { Name = "ZOLPIDEM TAB 10MG", Doses = "30 tablets" },
                new MedicineInfo() { Name = "PANTOPRAZOLE TAB 40MG", Doses = "30 tablets" }
            };
        }
    }
}

MedicineInfo 类代码:

public class MedicineInfo
{
    public MedicineInfo()
    {
        Prices = new List<StorePriceData>()
        {
            new StorePriceData() { StoreName = "xxxx" },
            new StorePriceData() { StoreName = "yyyy" },
            new StorePriceData() { StoreName = "zzzz" },
        };
    }

    public string Name { get; set; }
    public string Doses { get; set; }
    public List<StorePriceData> Prices { get; set; }
}

谢谢!

卡尔

【问题讨论】:

  • 为什么这是线程问题?单例是如何实现的?
  • 我实际上并不知道这是一个线程问题。由于这是 WPF,我相信事件发生在 UI 线程之外,并且需要做一些事情来从事件线程更新 UI 线程属性,但我不确定。
  • 不,UI 事件在调度程序线程上执行。

标签: c# wpf multithreading properties


【解决方案1】:

每次调用MedicineList 的getter 时,您都会返回一个新的List&lt;MedicineInfo&gt;

另外,Lists 并不是真正的单身人士。更好的实现如下所示:

public sealed class Lists
{
    private static readonly Lists _singleton = new Lists();

    private readonly List<MedicineInfo> _medicineList = new List<MedicineInfo>
    {
        new MedicineInfo() { Name = "ZOLPIDEM TAB 10MG", Doses = "30 tablets" },
        new MedicineInfo() { Name = "PANTOPRAZOLE TAB 40MG", Doses = "30 tablets" }
    };


    private Lists() { }

    public static Lists Singleton => _singleton;

    public List<MedicineInfo> MedicineList => _medicineList;
}

【讨论】:

  • 谢谢!这对我来说应该比以前更明显。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 2016-09-12
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多