【发布时间】:2015-10-21 01:48:13
【问题描述】:
在表格1中:
public MainWindow()
{
InitializeComponent();
Searcher searcher = new Searcher(this);
}
private delegate void NameCallBack(string varText, int varNumber);
public void UpdateTextBox(string input, int numberofdirsleft)
{
if (InvokeRequired)
{
textBox1.BeginInvoke(new NameCallBack(UpdateTextBox), new object[] { input });
}
else
{
textBox1.Text = input;
}
}
在一个新的班级顶部:
static MainWindow myform;
public Searcher(MainWindow form)
{
myform = form;
}
在foreach的新类中:
if (m_pars.IncludeSubDirsChecked)
{
DirectoryInfo[] subDirInfos = dirInfo.GetDirectories();
int counter = subDirInfos.Length;
foreach (DirectoryInfo subDirInfo in subDirInfos)
{
if (m_stop)
{
break;
}
// Recursion:
SearchDirectory(subDirInfo);
counter = counter - 1;
myform.UpdateTextBox(subDirInfo.FullName,counter);
}
}
如果在form1 中,我删除了int numberofdirsleft 和int varNumber,并且只使用它工作正常的字符串。
同样在不使用计数器变量的新类中。然后一切正常我看到textBox1中的文字。
例如在textBox1 我看到了:
c:\ c:\温度 c:\temp1....
但现在我想在textBox1 内的文本附近看到一个计数器:
26 c:\
25 c:\温度
24 c:\temp1
但是我现在使用添加的 int 变量的方式在 Program.cs 中抛出异常:
Application.Run(new MainWindow());
参数计数不匹配
System.Reflection.TargetParameterCountException was unhandled
IsTransient=false
Message=Parameter count mismatch.
Source=mscorlib
StackTrace:
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at FileSearcher.Program.Main() in e:\filesearch\FileSearcher\FileSearcher\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我在foreach 首先看到的变量计数器是 26
然后在foreach 内部,我看到使用计数器为 1 的断点
那么下次在foreach 计数器中是 26
【问题讨论】:
-
您能否编辑您的问题以包含整个
Main方法的代码?堆栈跟踪似乎没有准确地表明Program.cs中存在问题的代码行是您引用的代码行。