Invoke(delegateMethod)参数计数不匹配

JK_Crouse 2008-04-21 01:51:07
我在做一个后参线程修改窗体内文本框内容的试验,这个试验的程序在VS2003里应该是好用的,现在我用VS2005来写,有错误。下面是我的代码:

/*监视特定文件夹新否新建文件,如果新建文件事件发生,向文本框写字符串*/
using System;
using System.IO;


namespace WindowsApplication1
{
class monitor:System.IO.FileSystemWatcher
{
// Source directory
public string sourceDirectory
{
get { return this.Path; }
set { this.Path = value; }
}

public delegate void MyDelegate(string v);
public MyDelegate MyDelegateMethon = null;


public monitor(string srcDir)
{
// Set up the path to the source directory
sourceDirectory = srcDir;

// Set up the different properties
// Monitor all files and directories
this.Filter = "";
// Listen for changes in the name of files and directories
// and changes in date/time of the last modification.
this.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName |
System.IO.NotifyFilters.DirectoryName | System.IO.NotifyFilters.LastWrite)));
this.IncludeSubdirectories = true;
this.EnableRaisingEvents = true;

// Set up the handlers for the FileSystemWatcher events
this.Created += new FileSystemEventHandler(fsw_onCreated);

}

private void fsw_onCreated(object sender, System.IO.FileSystemEventArgs e)
{
MyDelegateMethon("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
}

/*窗体代码*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void setValue(String v)
{
textBox1.Text += v;
}

private void Form1_Load(object sender, EventArgs e)
{
monitor my = new monitor(@"D:\a");
my.MyDelegateMethon = new monitor.MyDelegate(setValue);
textBox1.Invoke(my.MyDelegateMethon);//运行时这里出现“参数计数不匹配”。
}
}
}

请问这个问题是怎么回事?怎么解决?

...全文
2634 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rocket2010 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 happinessboy 的回复:]

public delegate void MyDelegate(string v);
这是你的委托定义,匹配string类型参数
但是在你调用的地方,即 textBox1.Invoke(my.MyDelegateMethon)时,
没有传递参数,所以报错,
正确的做法是:Invoke(my.MyDelegateMethon,new object[]{"传递的值"})
[/Quote]

顶一下这个兄弟
JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
谢谢,我结帖了,接分。
JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
文本框里重复写字符串的问题解决了。把my放在Form1里声明。
HappinessBoy 2008-04-21
  • 打赏
  • 举报
回复
if (this.InvokeRequired)
------------>
if (this.TextBox1.InvokeRequired)
virusswb 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 JK_Crouse 的回复:]
我这样改的textBox1.Invoke(my.MyDelegateMethon,new object[]{""});
但是不行, textBox1.Text += v; 处出现“线程间操作无效: 从不是创建控件“textBox1”的线程访问它。”
[/Quote]要用委托来操作控件了,跨线程了
JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
能讲讲为什么这么用吗?
我这样改的》
private void setValue(String v)
{
if (this.InvokeRequired)
{
monitor my = new monitor(@"D:\a");
my.MyDelegateMethon = new monitor.MyDelegate(setValue);
this.Invoke(my.MyDelegateMethon, new object[] { v });
return;
}
textBox1.Text += v;
}

private void Form1_Load(object sender, EventArgs e)
{
monitor my = new monitor(@"D:\a");
my.MyDelegateMethon = new monitor.MyDelegate(setValue);
textBox1.Invoke(my.MyDelegateMethon, new object[] { ""});
}
}

但是文本框里写的不太对。

Created, D:\a\新建 文本文档 (7).txt
Created, D:\a\新建 文本文档 (8).txt
Created, D:\a\新建 文本文档 (8).txt
Created, D:\a\新建 文本文档 (9).txt
Created, D:\a\新建 文本文档 (9).txt
Created, D:\a\新建 文本文档 (9).txt
Created, D:\a\新建 文本文档 (9).txt
HappinessBoy 2008-04-21
  • 打赏
  • 举报
回复
如果调用textbox的线程不是创建textbox的线程,就会报错:从不是创建控件“textBox1”的线程访问它。”
private void SetValue(string text)
{
// InvokeRequired需要比较调用线程ID和创建线程ID
// 如果它们不相同则返回true
// ---InvokeRequired----
// 获取一个值,该值指示调用方在对控件进行方法调用的时候是否必须调用invoke方法,
// 因为调用方位于创建控件所在的线程以外的线程中.
if (this.textBox1.InvokeRequired)
{
my.MyDelegateMethon = new monitor.MyDelegate(SetValue);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}

private void Form1_Load(object sender, EventArgs e)
{
monitor my = new monitor(@"D:\a");
object o=@"d:\a";
Thread t = new Thread(new ParameterizedThreadStart(SetValue));
t.Start(o);//这里传你监听得到的值
}
北京的雾霾天 2008-04-21
  • 打赏
  • 举报
回复
这样改一下你的方法:


private void setValue(string v)
{
if (this.InvokeRequired)
{
MyDelegate m = new MyDelegate(setValue);
this.Invoke(m, new object[] { v});
return;
}
textBox1.Text += v;
}
JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
请注意一下,文本框中的字符串应该是从monitor监听的事件而来。
JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
我这样改的textBox1.Invoke(my.MyDelegateMethon,new object[]{""});
但是不行, textBox1.Text += v; 处出现“线程间操作无效: 从不是创建控件“textBox1”的线程访问它。”
ericzhangbo1982111 2008-04-21
  • 打赏
  • 举报
回复
using System;
using System.IO;


namespace WindowsApplication1
{
class monitor:System.IO.FileSystemWatcher
{
// Source directory
public string sourceDirectory
{
get { return this.Path; }
set { this.Path = value; }
}

public delegate void MyDelegate(string v);
public MyDelegate MyDelegateMethon = null;


public monitor(string srcDir)
{
// Set up the path to the source directory
sourceDirectory = srcDir;

// Set up the different properties
// Monitor all files and directories
this.Filter = "";
// Listen for changes in the name of files and directories
// and changes in date/time of the last modification.
this.NotifyFilter = ((System.IO.NotifyFilters)((System.IO.NotifyFilters.FileName ¦
System.IO.NotifyFilters.DirectoryName ¦ System.IO.NotifyFilters.LastWrite)));
this.IncludeSubdirectories = true;
this.EnableRaisingEvents = true;

// Set up the handlers for the FileSystemWatcher events
this.Created += new FileSystemEventHandler(fsw_onCreated);

}

private void fsw_onCreated(object sender, System.IO.FileSystemEventArgs e)
{
MyDelegateMethon("\r\n" + e.ChangeType + ", " + e.FullPath);
}
}
}

/*窗体代码*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void setValue(String v)
{
textBox1.Text += v;
}

private void Form1_Load(object sender, EventArgs e)
{
monitor my = new monitor(@"D:\a");
my.MyDelegateMethon = new monitor.MyDelegate(setValue);
object o=@"d:\a";
textBox1.Invoke(my.MyDelegateMethon,new object[]{o});//运行时这里出现“参数计数不匹配”。
}
}
}

请问这个问题是怎么回事?怎么解决?

JK_Crouse 2008-04-21
  • 打赏
  • 举报
回复
textBox1.Invoke(my.MyDelegateMethon);
使用的不是带有一个参数的方法吗?怎么参数计数不匹配呢?
HappinessBoy 2008-04-21
  • 打赏
  • 举报
回复
public delegate void MyDelegate(string v);
这是你的委托定义,匹配string类型参数
但是在你调用的地方,即 textBox1.Invoke(my.MyDelegateMethon)时,
没有传递参数,所以报错,
正确的做法是:Invoke(my.MyDelegateMethon,new object[]{"传递的值"})

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧