Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5dcf7cf

Browse files
author
guoxilin
committed
.netcore 中 Thread.Abort系列函数已经不再支持,结束时会报错,改之。
1 parent088e2ef commit5dcf7cf

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

‎integration/demo_running/behaviac/Base/socketconnect_base.cs‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
usingSystem.Collections.Generic;
2323
usingSystem.Net.Sockets;
2424
usingSystem.Runtime.InteropServices;
25+
usingSystem.Threading;
2526

2627
namespacebehaviac
2728
{
@@ -889,6 +890,7 @@ private void RecordText(string text)
889890

890891
privatevoidCreateAndStartThread()
891892
{
893+
s_tracerThreadCTS=newCancellationTokenSource();
892894
s_tracerThread=newSystem.Threading.Thread(MemTracer_ThreadFunc);
893895
s_tracerThread.Start();
894896
}
@@ -1080,7 +1082,9 @@ protected bool ReceivePackets(string msgCheck)
10801082
privatevoidThreadFunc()
10811083
{
10821084
Log("behaviac: Socket Thread Starting\n");
1083-
1085+
1086+
CancellationTokentoken=s_tracerThreadCTS.Token;
1087+
10841088
try
10851089
{
10861090
this.ReserveThreadPacketBuffer();
@@ -1092,36 +1096,39 @@ private void ThreadFunc()
10921096

10931097
try
10941098
{
1095-
serverSocket=SocketBase.Create(blockingSocket);
1096-
1097-
if(serverSocket==null)
1099+
if(!token.IsCancellationRequested)
10981100
{
1099-
Log("behaviac: Couldn't create server socket.\n");
1100-
return;
1101-
}
1101+
serverSocket=SocketBase.Create(blockingSocket);
1102+
1103+
if(serverSocket==null)
1104+
{
1105+
Log("behaviac: Couldn't create server socket.\n");
1106+
return;
1107+
}
11021108

1103-
stringbufferTemp=string.Format("behaviac: Listening at port {0}...\n",m_port);
1104-
Log(bufferTemp);
1109+
stringbufferTemp=string.Format("behaviac: Listening at port {0}...\n",m_port);
1110+
Log(bufferTemp);
11051111

1106-
// max connections: 1, don't allow multiple clients?
1107-
if(!SocketBase.Listen(serverSocket,m_port,1))
1108-
{
1109-
Log("behaviac: Couldn't configure server socket.\n");
1110-
SocketBase.Close(refserverSocket);
1111-
return;
1112+
// max connections: 1, don't allow multiple clients?
1113+
if(!SocketBase.Listen(serverSocket,m_port,1))
1114+
{
1115+
Log("behaviac: Couldn't configure server socket.\n");
1116+
SocketBase.Close(refserverSocket);
1117+
return;
1118+
}
11121119
}
11131120
}
11141121
catch(Exceptionex)
11151122
{
11161123
Debug.LogError(ex.Message);
11171124
}
11181125

1119-
while(m_terminating.Get()==0)
1126+
while(m_terminating.Get()==0&&!token.IsCancellationRequested)
11201127
{
11211128
//wait for connecting
11221129
while(m_terminating.Get()==0)
11231130
{
1124-
if(SocketBase.TestConnection(serverSocket))
1131+
if(SocketBase.TestConnection(serverSocket)||token.IsCancellationRequested)
11251132
{
11261133
break;
11271134
}
@@ -1133,6 +1140,11 @@ private void ThreadFunc()
11331140
{
11341141
Log("behaviac: accepting...\n");
11351142

1143+
if(token.IsCancellationRequested)
1144+
{
1145+
break;
1146+
}
1147+
11361148
try
11371149
{
11381150
m_writeSocket=SocketBase.Accept(serverSocket,SocketConnection.kSocketBufferSize);
@@ -1149,6 +1161,11 @@ private void ThreadFunc()
11491161
Debug.LogError(ex.Message);
11501162
}
11511163

1164+
if(token.IsCancellationRequested)
1165+
{
1166+
break;
1167+
}
1168+
11521169
try
11531170
{
11541171
m_isConnected.AtomicInc();
@@ -1167,18 +1184,23 @@ private void ThreadFunc()
11671184
{
11681185
Debug.LogError(ex.Message);
11691186
}
1187+
1188+
if(token.IsCancellationRequested)
1189+
{
1190+
break;
1191+
}
11701192

11711193
try
11721194
{
1173-
while(m_terminating.Get()==0&&this.m_writeSocket!=null)
1195+
while(m_terminating.Get()==0&&this.m_writeSocket!=null&&!token.IsCancellationRequested)
11741196
{
11751197
System.Threading.Thread.Sleep(1);
11761198

11771199
this.SendAllPackets();
11781200
this.ReceivePackets("");
11791201
}
11801202

1181-
if(this.m_writeSocket!=null&&this.m_writeSocket.Connected)
1203+
if(this.m_writeSocket!=null&&this.m_writeSocket.Connected&&!token.IsCancellationRequested)
11821204
{
11831205
// One last time, to send any outstanding packets out there.
11841206
this.SendAllPackets();
@@ -1406,6 +1428,7 @@ protected struct PacketsStats
14061428
publicintinit;
14071429
};
14081430

1431+
CancellationTokenSources_tracerThreadCTS;
14091432
privateSystem.Threading.Threads_tracerThread;
14101433
protectedstringms_texts;
14111434
protectedvolatileboolm_bHandleMessage;
@@ -1496,7 +1519,9 @@ public void Close()
14961519

14971520
if(s_tracerThread.IsAlive)
14981521
{
1499-
s_tracerThread.Abort();
1522+
s_tracerThreadCTS.Cancel(true);
1523+
s_tracerThreadCTS.Dispose();
1524+
s_tracerThreadCTS=null;
15001525
}
15011526

15021527
s_tracerThread=null;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp