首页 新闻 会员 周边

C# 怎样在子线程里调用引入的DLL里得方法?

0
悬赏园豆:10 [已解决问题] 解决于 2011-10-18 19:07
using SSQ_BLL;
using System.Threading;

namespace SkySea_QQ_0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool isRec = true;
string recTxt = "";
Thread T;
private void bt_connection_Click(object sender, EventArgs e)
{
MyBLL.getConn(txt_ip.Text ,txt_port .Text);
MyBLL mybll = new MyBLL();
T = new Thread(mybll.receiveMessage);
T.Start();
while (isRec)
{
recTxt = mybll.receiveMessage();
txt_receive.AppendText(recTxt + "\r\n");
}
bt_connection.Enabled = false;
}


}
}

在练习着用三层写一个小东西,在表示层开一个子线程,调用业务逻辑层自己写的一个SSQ_BLL.dll里写的一个方法,先MyBLL mybll = new MyBLL();在表示层写线程调用 T = new Thread(mybll.receiveMessage);时,mybll.receiveMessage这样写出现错误,怎样在子线程里调用引入的DLL里得方法????请指教!

以下是SSQ_BLL.dll里面Mybll类里得receiveMessage方法

///<summary>
/// 用于接收信息的方法
///</summary>
///<returns>返回单次接收到的字符串</returns>
public string receiveMessage()
{
byte[] mybyte = new byte[1024 * 1024 * 1024];
try
{
socketClient.Receive(mybyte);
string mytxt = System.Text.Encoding.Default.GetString(mybyte);
return mytxt;
}
catch (Exception ex)
{
writeLog(ex.Message);
throw new Exception(ex.Message);
}
}

 

VincentZhu的主页 VincentZhu | 初学一级 | 园豆:166
提问于:2011-10-17 19:38
< >
分享
最佳答案
1

使用多线程的时候,不能有返回值。另外,你调用的方式也有问题呀。已经在一个线程里面进行调用了(函数结束后,线程会自动退出),下面又进行了调用,处理数据的逻辑就错了。

1 public void StartProcess()
2 {
3 while (isRec)
4 {
5 recTxt = mybll.receiveMessage();
6 txt_receive.AppendText(recTxt + "\r\n");
7 }
8 }


调用时候应该是

T = new Thread(StartProcess);

T.Start();

bt_connection.Enabled = false;
这里要注意的是
txt_receive.AppendText(recTxt + "\r\n"); 涉及到UI更新建议使用委托,我就不替你更改了。
希望对你有帮助。
收获园豆:8
归真 | 小虾三级 |园豆:605 | 2011-10-18 16:48

谢谢!

VincentZhu | 园豆:166 (初学一级) | 2011-10-18 19:11
其他回答(2)
0

出现什么错误提示?

收获园豆:1
dudu | 园豆:30994 (高人七级) | 2011-10-17 20:18
0

  子线程调用的方法不能有返回值

收获园豆:1
华-子 | 园豆:156 (初学一级) | 2011-10-18 18:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册