我在树视图中遇到问题,我尝试根据 Node.Text 索引添加子节点(我也根据 int 索引尝试过这个 - 无济于事)。当同步运行时,这非常有效。但是,我运行完全相同的 Async (backgroundWorker) 会引发未处理的 ArgumentOutOfRange 异常。另一个奇怪的部分是我试图在两个不同的领域捕捉到这个异常。见代码:
using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
int x = 0;
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
foreach (string a in (string[])subkey.GetValue("Users", ""))
{
User u = new User(a);
usrs.addUser(new User(a));
wgs.addUserToWorkgroup(subkey_name, a);
usrs.AddWorkGroupToUser(subkey_name, a);
int trycount = 0;
TryAgain:
try
{
//here is where the exception occurs
ExecuteSecure(() => treeView1.Nodes[subkey_name].Nodes.Add(a, a));
}
catch (ArgumentOutOfRangeException)//This does not catch it.
{
trycount++;
if (trycount < 100)
{
goto TryAgain; //b/c I cannot catch it this never happens...
}
}
}
}
x++;
//System.Threading.Thread.Sleep(2);
//As you can see I've tried to let the tread sleep to resolve this
//- it will get a little farther but still eventually bomb out.
}
}
这是 ExecuteSecure 代码(https://stackoverflow.com/a/8021020/1387186)
private void ExecuteSecure(Action a)
{
o = new object();
try
{
if (InvokeRequired)
{
lock (o)
{
BeginInvoke(a);
}
}
else
a();
}
catch (Exception) //again **sigh** this does not catch the error
{ }