I have a Windows Forms application that I'm working on, but I'm having trouble when I start the application. The application should load saved information from a config file, and then check for new items. When I start the application, it starts looking for new items before it has finished loading saved items. Because of this the user is alerted of new items that are not really new, they just have not been loaded from the file yet.
The Form:
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();
}
}
The other object:
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());
}
}
}
Should I have object A call it's functions on a new thread, and lock so CheckForUpdates cannot be called if LoadItems is running, or is there a simpler way to do this that I'm missing?
EDIT:开发者_StackOverflow
I found the problem. I was clearing the List of items (so it would not grow forever), but I was only filling it with the newly found items. So every time I ran the application only the newest items where in the list, and all the older items were flushed out.
STUPID!!!
Thanks for the help though, and sorry for the crappy question.
Is there any reason why the check is not in the constructor?
public MainForm()
{
InitializeComponent();
a = new A();
a.ItemsFound += new A.NewItemsFoundEventHandler(a_FoundItems);
a.ItemsLoaded += new A.ItemsLoadedEventHandler(a_ItemsLoaded);
a.LoadItems();
a.CheckForUpdates();
}
Hmm, from the code you've posted i dont see a problem, especially assuming all this runs on the ui thread.. can you post the code for the loading of items?
Perhaps the loading it self is firing the ItemsFound
event? You could do the subscription for ItemsFound
in the eventhandler for ItemsLoaded
instead of in the constructor.
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();
}
}
精彩评论