怎样使用call方法回调方法传递参数
展开全部
写一个类,将你要的参数都封装进去。然后通过IAsyncResult.AsyncState进行传递。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 1)定义类,封装你要的任何东西
class StateObject
{
public Scoekt frd{get;set;}
pubic ComboBox combo{get;set;}
//可以添加任何你需要的东西
//……
}
// 2)在异步调用前,实例化StateObject
StateObject state = new StateObject();
state.frd = frd;
state.combo = comBox_client;
AsyncCallback callBack = new AsyncCallback(sendCallBack);
frd.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, callBack, state);
//3)在回调函数中使用StateObject
private void sendCallBack(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
//从 state中 取出frd, combo ……
Socket frd = state.frd;
ComboBox combo = state.combo;
try
{
frd.EndSend(ar);
}
catch (System.Exception ex)
{
combo.Invoke(removeFriend, combo, friends,frd)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 1)定义类,封装你要的任何东西
class StateObject
{
public Scoekt frd{get;set;}
pubic ComboBox combo{get;set;}
//可以添加任何你需要的东西
//……
}
// 2)在异步调用前,实例化StateObject
StateObject state = new StateObject();
state.frd = frd;
state.combo = comBox_client;
AsyncCallback callBack = new AsyncCallback(sendCallBack);
frd.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, callBack, state);
//3)在回调函数中使用StateObject
private void sendCallBack(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
//从 state中 取出frd, combo ……
Socket frd = state.frd;
ComboBox combo = state.combo;
try
{
frd.EndSend(ar);
}
catch (System.Exception ex)
{
combo.Invoke(removeFriend, combo, friends,frd)
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询