2011-06-10 33 views
1

我有一个Windows窗体应用程序,但我在启动应用程序时遇到了问题。应用程序应该从配置文件中加载保存的信息,然后检查新项目。当我启动应用程序时,它会在完成加载保存的项目之前开始查找新项目。因此,用户会收到不是真正新的新项目的警报,但它们尚未从文件加载。控制应用程序流程的问题

的形式为:

public class MainForm : Form 
{ 
    A a; 

    public MainForm() 
    { 
     InitializeComponent(); 
     a = new A(); 
     a.ItemsFound += new A.NewItemsFoundEventHandler(a_FoundItems); 
     a.ItemsLoaded += new A.ItemsLoadedEventHandler(a_ItemsLoaded); 
     a.LoadItems(); 
    } 

    public void a_FoundItems(object sender, EventArgs e) 
    { 
     //Alert user of new items. 
    } 
    public void a_ItemsLoaded(object sender, EventArgs e) 
    { 
     //Update GUI with items loaded from file. 
     this.UpdateTheGUI_ThisIsNotARealMethodInMyProgram(); 

     //Then look for new items. 
     a.CheckForUpdates(); 
    } 
} 

其他对象:

public class A 
{ 
    public A(){} 

    public void LoadItems() 
    { 
     //Load Items from save file... 
     OnItemsLoaded(this); 
    } 

    public void CheckForUpdates() 
    { 
     //Check for new items... 
     //If new items are found, raise ItemsFound event 
     OnNewItemsFound(this,new EventArgs()); 
    } 

    public delegate void NewItemsFoundEventHandler(object sender, EventArgs e); 
    public event NewItemsFoundEventHandler ItemsFound; 
    protected void OnNewItemsFound(object sender, EventArgs e) 
    { 
     if(ItemsFound != null) 
     { 
      ItemsFound(sender,e); 
     } 
    } 

    public delegate void ItemsLoadedEventHandler(object sender, EventArgs e); 
    public event ItemsLoadedEventHandler ItemsLoaded; 
    protected void OnItemsLoaded(object sender) 
    { 
     if(ItemsLoaded != null) 
     { 
      ItemsLoaded(sender,new System.EventArgs()); 
     } 
    } 
} 

我应该有对象上的新线程调用它的功能,并锁定所以如果LoadItems运行CheckForUpdates不能叫,还是有更简单的方法来做到这一点,我错过了?

编辑:

我发现这个问题。我正在清理项目清单(所以它不会永远增长),但我只是用新找到的项目填充它。所以每次运行应用程序时,只有列表中的最新项目以及所有较旧的项目被刷新。

笨蛋!

感谢您的帮助,并抱歉蹩脚的问题。

+1

加载处理运行时禁用应用程序输入? – kenny 2011-06-10 14:50:55

回答

2

是否有任何理由为什么检查不在构造函数中?

public MainForm() 
{ 
    InitializeComponent(); 
    a = new A(); 
    a.ItemsFound += new A.NewItemsFoundEventHandler(a_FoundItems); 
    a.ItemsLoaded += new A.ItemsLoadedEventHandler(a_ItemsLoaded); 
    a.LoadItems(); 
    a.CheckForUpdates(); 
} 
+0

本来我就是这么想的,但它的行为方式相同,所以我将它移至a_ItemsLoaded方法来尝试修复问题。 – Tester101 2011-06-10 14:58:05

+0

将断点添加到'CheckForUpdates()','a_FoundItems(...)'和'a_ItemsLoaded(...)'。到达这些断点时检查调用堆栈。 – mgronber 2011-06-10 15:02:41

+0

设置断点帮助我意识到我的愚蠢。谢谢您的帮助。 – Tester101 2011-06-10 15:12:44

2

嗯,从你的代码已经发布我没有看到一个问题,特别是假设所有这些运行在UI线程..你可以张贴代码项目的加载?

也许它自己加载的是触发ItemsFound事件?您可以在ItemsLoaded的事件处理程序中执行ItemsFound的订阅,而不是在构造函数中。

public class MainForm : Form 
{ 
    A a; 
    public MainForm() 
    { 
     InitializeComponent(); 
     a = new A(); 
     a.ItemsLoaded += new A.ItemsLoadedEventHandler(a_ItemsLoaded); 
     a.LoadItems(); 
    } 

    public void a_ItemsLoaded(object sender, EventArgs e) 
    { 
     a.ItemsFound += new A.NewItemsFoundEventHandler(a_FoundItems); 
     a.CheckForUpdates(); 
    } 
}