关于委托的问题。。

skevil 2013-08-22 10:22:47
 Thread TRec;
private void DataRecording_Load(object sender, EventArgs e)
{

TRec = new Thread (recording );
TRec.IsBackground = true;
TRec.Start(textBox1.Text );

}
bool flag = true;

//委托
private delegate void rec(string s);
private void recording()
{
if (InvokeRequired)
{
Invoke(new rec(Msg ));
}
else
{
if (mainys.alive == true)
{

while (flag)
{
for (int i = 0; i < mainys.ArrStrMsgID.Count; i++)
{
Msg(mainys.ArrStrMsgID[i].ToString() + "收到了" + mainys.Arrnum[i] + "个文件,接收时间为:");
foreach (object a in mainys.ArrRec)
{
string id = a.ToString().Substring(0, 5);
if (id == mainys.ArrStrMsgID[i].ToString())
{
Msg(" " + a.ToString().Substring(5));
}

}
}
Thread.Sleep(50);
textBox1.Clear();
}




}
else if (main.alive == true)
{
main m = new main();
}


TRec.Abort();
}



}


private void Msg(string s)
{
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
}

private void button1_Click(object sender, EventArgs e)
{

flag = false;
TRec.Abort();
this.Close();

}

为什么在 if (InvokeRequired)
{
Invoke(new rec(Msg ));
}

这里说我的参数计数不匹配?我Msg(string s)是string的咧,private delegate void rec(string s);委托的时候也是string,为啥不匹配?哪里错了呢?求指教
...全文
392 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
是根据5楼代码改吗?
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 5 楼 hwenycocodq520 的回复:
大致修改成这样试试,


private delegate void rec(string s);

private void recordingText(string text)
{
if (textBox1.InvokeRequired)
{
Invoke(new rec(recordingText), new object[] { text });
}
else
{
Msg(text);
}
}

private void recording()
{
while (flag)
{
for (int i = 0; i < mainys.ArrStrMsgID.Count; i++)
{
recordingText(mainys.ArrStrMsgID[i].ToString() + "收到了" + mainys.Arrnum[i] + "个文件,接收时间为:");
foreach (object a in mainys.ArrRec)
{
string id = a.ToString().Substring(0, 5);
if (id == mainys.ArrStrMsgID[i].ToString())
{
recordingText(" " + a.ToString().Substring(5));
}
}
}
Thread.Sleep(50);
}
}

private void Msg(string s)
{
textBox1.AppendText(s);
textBox1.AppendText("\r\n");
}




不行诶
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
大致修改成这样试试,


private delegate void rec(string s);

private void recordingText(string text)
{
    if (textBox1.InvokeRequired)
    {
        Invoke(new rec(recordingText), new object[] { text });
    }
    else
    {
        Msg(text);
    }
}

private void recording()
{
     while (flag)
     {
         for (int i = 0; i < mainys.ArrStrMsgID.Count; i++)
         {
             recordingText(mainys.ArrStrMsgID[i].ToString() + "收到了" + mainys.Arrnum[i] + "个文件,接收时间为:");
             foreach (object a in mainys.ArrRec)
             {
                 string id = a.ToString().Substring(0, 5);
                 if (id == mainys.ArrStrMsgID[i].ToString())
                 {
                     recordingText("                              " + a.ToString().Substring(5));
                 }
             }
          }
         Thread.Sleep(50);
     }
}

private void Msg(string s)
{
     textBox1.AppendText(s);
     textBox1.AppendText("\r\n");
 }
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复

Invoke(new rec(Msg),new object[]{textBox1.Text});
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
看错了,这里"Msg 返回 void 类型 rec(void)" 发表错了,你那里调用没错
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 1 楼 hwenycocodq520 的回复:
Msg 返回 void 类型 rec(void) ? what the fuck
呃。。。能教下我吗?
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
Msg 返回 void 类型 rec(void) ? what the fuck
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 20 楼 hwenycocodq520 的回复:
TRec.Abort();你在哪里调用的呢?
谢谢,知道了,非常感谢你的帮忙
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
TRec.Abort();你在哪里调用的呢?
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 17 楼 hwenycocodq520 的回复:
谁叫你在recording直接操作textBox1啊,
终于有点开始小明白了。。。。 我关闭这个子窗体时说无法访问已释放的对象 该怎么停止访问咧?
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
最省事的方法,在你需要清除的地方调用recordingText("")(5楼), ShowCardInfo("")(8楼)

if (!string.IsNullOrEmpty(data))
{
    textBox1.AppendText(data);
}
else
{
    textBox1.Text="";
}
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
谁叫你在recording直接操作textBox1啊,
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 14 楼 hwenycocodq520 的回复:
上面的和5楼的有啥区别?
发现还是一样会报错了。。。
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 13 楼 skevil 的回复:
[quote=引用 8 楼 corner_hzd 的回复:]
 

private void recording()
{
     while (flag)
     {
         for (int i = 0; i < mainys.ArrStrMsgID.Count; i++)
         {
             recordingText(mainys.ArrStrMsgID[i].ToString() + "收到了" + mainys.Arrnum[i] + "个文件,接收时间为:");
             foreach (object a in mainys.ArrRec)
             {
                 string id = a.ToString().Substring(0, 5);
                 if (id == mainys.ArrStrMsgID[i].ToString())
                 {
                     ShowCardInfo("                              " + a.ToString().Substring(5));
                 }
             }
          }
         Thread.Sleep(50);
     }
}

 private delegate void ShowCardInfoDelegate(string data);
       private void ShowCardInfo(string data)
        {
            if (textBox1.InvokeRequired)
            {
                var d = new ShowCardInfoDelegate(ShowCardInfo);
                Invoke(d, data);
            }
            else
            {
                if (!string.IsNullOrEmpty(data))
                {
                   textBox1.AppendText(data);
                }
            }
        }
谢谢,我终于搞好了,但是我发现要是我在线程recording里面将textBox1的内容清除了就会报错,这是为什么呢?[/quote] 呃,不好意思,我发现好像还是有问题,我往里面textBox写东西的时候就会说 线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
智商余额不足 2013-08-22
  • 打赏
  • 举报
回复
上面的和5楼的有啥区别?
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 8 楼 corner_hzd 的回复:
 

private void recording()
{
     while (flag)
     {
         for (int i = 0; i < mainys.ArrStrMsgID.Count; i++)
         {
             recordingText(mainys.ArrStrMsgID[i].ToString() + "收到了" + mainys.Arrnum[i] + "个文件,接收时间为:");
             foreach (object a in mainys.ArrRec)
             {
                 string id = a.ToString().Substring(0, 5);
                 if (id == mainys.ArrStrMsgID[i].ToString())
                 {
                     ShowCardInfo("                              " + a.ToString().Substring(5));
                 }
             }
          }
         Thread.Sleep(50);
     }
}

 private delegate void ShowCardInfoDelegate(string data);
       private void ShowCardInfo(string data)
        {
            if (textBox1.InvokeRequired)
            {
                var d = new ShowCardInfoDelegate(ShowCardInfo);
                Invoke(d, data);
            }
            else
            {
                if (!string.IsNullOrEmpty(data))
                {
                   textBox1.AppendText(data);
                }
            }
        }
谢谢,我终于搞好了,但是我发现要是我在线程recording里面将textBox1的内容清除了就会报错,这是为什么呢?
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 7 楼 hwenycocodq520 的回复:
是根据5楼代码改吗?
嗯,对啊,然后就是出现那种报错了
skevil 2013-08-22
  • 打赏
  • 举报
回复
引用 10 楼 u011384967 的回复:

TRec.Start(textBox1.Text );//这样能行?

 Invoke(new rec(Msg)); //传递了方法引用,为什么不传递参数?
TRec.Start(textBox1.Text );这句是我那时复制上来的时候忘记删掉的了,之前是没有的,这个我是按网上的例子试着改的
Phoozyan 2013-08-22
  • 打赏
  • 举报
回复

TRec.Start(textBox1.Text );//这样能行?

 Invoke(new rec(Msg)); //传递了方法引用,为什么不传递参数?
syn07471 2013-08-22
  • 打赏
  • 举报
回复
你调用出错了 Invoke你只传了委托实例,但是少了参数。 Invoke(new rec(Msg),new object[]{"111"});
加载更多回复(1)

110,567

社区成员

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

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

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