I'm creating menu items in a separate thread and adding them to the menu created in the main thread. I'm using Invoke for that. Getting "Value does not fall within the expected range" exception.
//creating new thread
Thread thread = new Thread(LoadRecentTasks);
thread.IsBackground = true;
thread.Start();
private void LoadRecentTasks()
{
EntryCollection recentEntries = Entry.GetRecentEntries(10);
foreach (Entry entry in recentEntries)
{
MenuItemPlus menuItem = new MenuItemPlus();
menuItem.Text = entry.GetShortDescription(28);
menuItem.Click += recentTasksMenuItem_Click;
menuItem.Tag = entry;
AddRecentMenuItem(menuItem);
}
}
private void AddRecentMenuItem(MenuItemPlus menuItem)
{
if (InvokeRequired)
{
BeginInvoke(new CallbackDelegate<MenuItemPlus>(AddRecentMenuItem), menuItem);
}
else
{
menuItemRecent.MenuItems.Add(menuItem); //<-- exception thrown here
}
}
delegate void CallbackDelegate<T>(T t);
Any suggestions?
UPDATE: i've tried it with Invoke too - same result.
menuItemRecent is created as part of the form's initialization routine. The thread is started on form's Activated event