的任务.运行内部代码未执行
本文关键字:执行 代码 内部 任务 运行 | 更新日期: 2023-09-27 18:11:49
我正在编写一个服务器-客户端,并希望使用Task.Run(() => {SomeCode});
运行操作这是我的代码
Task.Run(() =>
{
_control_com_peer2peer.VerifyConnection(port: 6001, IpAddressClient: ComboBoxRemoteIP.Text);
});
内部代码基本上是
public void VerifyConnection(int port, string IpAddressClient)
{
Control_PeerSessionListCheckCreate(RemoteIP: IpAddressClient);
Domain_Peer2PeerCom.peerSessionList.Find(x => x.remoteIP == IpAddressClient).CommandLocker("Hello");
Domain_Peer2PeerCom.PeerSessionChangeIsConnected(IpAddressClient, true);
}
我在VerifyConnection
中有断点,但它们都没有着火。
编辑
触发任务按钮的完整代码
private void Connect_Click(object sender, EventArgs e)
{
string s_RemoteIP;
if (ComboBoxRemoteIP.Text != "")
{
if (Network.startPing(ComboBoxRemoteIP.Text))
{
Control_Peer2peerCom.Control_PeerSessionListCheckCreate(ComboBoxRemoteIP.Text);
//bool IspeerReal = Control_Peer2peerCom.peerSessionListGet(ComboBoxRemoteIP.Text, out peerSessionCurrentParam);
if (/*IspeerReal&&*/!(Control_Peer2peerCom.Control_PeerSessionCheckIsConnected(ComboBoxRemoteIP.Text)))//peerSessionCurrentParam.IsConnetcted))//(!Peer2peerCom.isPeerConnetionVerifaied)
{
SelectiveEnable();
tbStatus.Text = "Wait...";
tbStatus.BackColor = Color.Yellow;
s_RemoteIP = ComboBoxRemoteIP.Text;
Task.Run(() =>
{
_control_com_peer2peer.VerifyConnection(port: 6001, IpAddressClient:
ComboBoxRemoteIP.Text);
});
//more code here , Not related
}
当然,如果我在Task.Run
之外运行这个方法它会运行良好
原因是什么
问题是在task.run当然,一旦我改变为简单的线程异常抛出的交叉线程!我的错误是将ComboBoxRemoteIP.Text
发送到任务
任务没有任何异常,所以我希望能够捕获问题
我更正它的方法就是添加String Ipddress = ComboBoxRemoteIP.Text
和发送字符串代替