public partial class MainForm : Form
{
private delegate void FileSearchStartDelegate(string path,string zifuchuan);
private FileSearchStartDelegate FileSreachStart; private delegate void FilesAddDelegate(string file);
private FilesAddDelegate FilesAdd; public MainForm()
{
InitializeComponent(); FileSreachStart = new FileSearchStartDelegate(StartSreachFile);
FilesAdd = new FilesAddDelegate(AddFiles);
}
/// <summary>
/// 搜索指定目录,填充界面列表
/// </summary>
private void btSearch_Click(object sender, EventArgs e)
{
string path = this.tbMuLu.Text;
string zifuchuan = this.tbString.Text;
this.FileSearch(path, zifuchuan);
} private void FileSearch(string path,string zifuchuan)
{
this.BeginInvoke(FileSreachStart, new object[] { path, zifuchuan });
} private void StartSreachFile(string path, string zifuchuan)
{
for (int i = 0; i <= 1000000; i++)
{
string file =i.ToString()+ "a.txt";
Application.DoEvents();
this.Invoke(FilesAdd, new object[] { file });
}
} private void AddFiles(string file)
{
this.FileListLB.Items.Add(file);
}
}
这并不是一个多线程问题,但是为什么在填充界面的时候,点击退出,会发生错误?
错误提示:
未处理 System.Reflection.TargetInvocationException
Message="调用的目标发生了异常。"
Source="mscorlib"
StackTrace:
在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
在 System.Delegate.DynamicInvokeImpl(Object[] args)
在 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
在 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
在 System.Threading.ExecutionContext.runTryCode(Object userData)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
在 System.Windows.Forms.Control.InvokeMarshaledCallbacks()
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 多线程测试.Program.Main() 位置 E:\测试\多线程测试\多线程测试\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
不是很明白为什么会这样?期盼各位高人的答复!
{
private delegate void FileSearchStartDelegate(string path,string zifuchuan);
private FileSearchStartDelegate FileSreachStart; private delegate void FilesAddDelegate(string file);
private FilesAddDelegate FilesAdd; public MainForm()
{
InitializeComponent(); FileSreachStart = new FileSearchStartDelegate(StartSreachFile);
FilesAdd = new FilesAddDelegate(AddFiles);
}
/// <summary>
/// 搜索指定目录,填充界面列表
/// </summary>
private void btSearch_Click(object sender, EventArgs e)
{
string path = this.tbMuLu.Text;
string zifuchuan = this.tbString.Text;
this.FileSearch(path, zifuchuan);
} private void FileSearch(string path,string zifuchuan)
{
this.BeginInvoke(FileSreachStart, new object[] { path, zifuchuan });
} private void StartSreachFile(string path, string zifuchuan)
{
for (int i = 0; i <= 1000000; i++)
{
string file =i.ToString()+ "a.txt";
Application.DoEvents();
this.Invoke(FilesAdd, new object[] { file });
}
} private void AddFiles(string file)
{
this.FileListLB.Items.Add(file);
}
}
这并不是一个多线程问题,但是为什么在填充界面的时候,点击退出,会发生错误?
错误提示:
未处理 System.Reflection.TargetInvocationException
Message="调用的目标发生了异常。"
Source="mscorlib"
StackTrace:
在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
在 System.Delegate.DynamicInvokeImpl(Object[] args)
在 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
在 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
在 System.Threading.ExecutionContext.runTryCode(Object userData)
在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
在 System.Windows.Forms.Control.InvokeMarshaledCallbacks()
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 多线程测试.Program.Main() 位置 E:\测试\多线程测试\多线程测试\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
不是很明白为什么会这样?期盼各位高人的答复!
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 搜索指定目录,填充界面列表
/// </summary>
private void btSearch_Click(object sender, EventArgs e)
{
string path = this.tbMuLu.Text;
string zifuchuan = this.tbString.Text;
this.FileSearch(path, zifuchuan);
} private void FileSearch(string path,string zifuchuan)
{
for (int i = 0; i <= 1000000; i++)
{
string file =i.ToString()+ "a.txt";
Application.DoEvents();
this.FileListLB.Items.Add(file);
}
}
}
这样做才应该是你要的效果