如何从所有打开的表单中访问单实例应用程序中属性的最后一个值?

问题描述

我有一个下载器应用程序,该应用程序具有MainForm,它是单个实例,而StartDownloadForm可以具有多个实例。如果开始下载,则使用NativeMessage从Chrome扩展程序调用该exe。如果MainForm已经打开,则不会重新打开另一个。但是StartDownloadForm可以一次又一次打开以开始任何下载。我使用添加新的下载到MainForm.ListView DownloadList.MTDOList静态属性,用于存储所有下载内容,我想从通过两次或多次调用exe打开的所有StartDownloadForm中访问该属性的最后一个值。但是对于StartDownloadForm的新实例来说,它是空的。我该如何解决这个问题?

[STAThread]
private static void Main(string[] args)
{
    bool creatednew;

    var m = new Mutex(true,"myApp",out creatednew);

    
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    
    var jsondata = 
        Receiver.ProcessMessage(Receiver.Read());
    var msg = JsonConvert.DeserializeObject<DownloadMessage>(jsondata);
    
    //Open form independently from Main thread
    var thread = new Thread(() =>
                                Application.Run(new StartDownloadForm(msg)));
    thread.TrySetApartmentState(ApartmentState.STA); 
    thread.Start(); 
    
    //Single instance MainForm
    if (creatednew)
    {
        Application.Run(new MainForm());
    }

}

public static class DownloadList
{
    public static List<MultiThreadDownloadOrganizer> MTDOList{ get; set; }
    
    public static void AddOrUpdate(MultiThreadDownloadOrganizer mtdo)
    {
        if (MTDOList == null)
            MTDOList = new List<MultiThreadDownloadOrganizer>();
        if (MTDOList.All(x => x.Id != mtdo.Id))
            MTDOList.Add(mtdo);
    }
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)