Home > Net >  Would you please tell me why your bosses, TCP a thread, a thread processing, can appear the situatio
Would you please tell me why your bosses, TCP a thread, a thread processing, can appear the situatio

Time:11-13



Private Queue queueTCPCacheReceived;
Private Thread threadTCPCacheReceived;
The static System. When the Timer Timer2;


Private void ReceiveInformation2 (Object state)//timer is used to receive data
{
Try
{
Byte [] readBuffer=new byte [the sock ReceiveBufferSize];
Int count=the sock. The Receive (readBuffer);
The lock (queueTCPCacheReceived. SyncRoot)
{
for (int i=0; i {
QueueTCPCacheReceived. The Enqueue (readBuffer [I]);
}
}
}
The catch (Exception ex)
{
String EM=ErrorMessage (ex);
Timer1. Enabled=false;//UI stop update
Timer2. Change (Timeout. Infinite, Timeout. Infinite);
The sock. The Close ();
MessageBox. Show (EM, "error");
return;
}
}
Timer2. Change (0, the Timeout. Infinite);
}


Private void ThreadTCPCacheReceivedFunction ()
{
List ListData=https://bbs.csdn.net/topics/new List ();
While (the sock!=null & amp; & The sock. Connected)
{
Int cacheLength=queueTCPCacheReceived. Count;
Console. WriteLine (cacheLength. ToString ());
If (cacheLength & gt; 0)
{
The lock (queueTCPCacheReceived. SyncRoot)
{
for (int i=0; i {
If (listData. Count==0)
{
If ((byte) queueTCPCacheReceived. Dequeue ()==0 x55 & amp; & (byte) queueTCPCacheReceived. Dequeue ()==0 x00)
{
X55 listData. Add (0);
ListData. Add (0 x00);
}
}
Else if (listData. Count & gt; 0)
{
ListData. Add ((byte) queueTCPCacheReceived. Dequeue ());
}

If (listData. Count==896)
{
The lock (queueTCPCacheReceived. SyncRoot)
{
Byte [] byteData=(https://bbs.csdn.net/topics/listData.ToArray);
If (byteData [0]==85 & amp; & ByteData [895]==238)/validation/fore and aft
{
If (nlink_linktrack_anchor_frame0. UnpackAnchorFrame0Data (byteData, byteData. Length))
{
ErrorCount=0;
}
}
}
ListData. The Clear ();
}
}
}
}
}
QueueTCPCacheReceived. The Clear ();
}

CodePudding user response:


When you receive and handle hasn't returned yet, so...

CodePudding user response:

reference 1st floor tangyanzhi1111 response:
when you receive, processing hasn't returned yet, so...
does not return,

CodePudding user response:

As if cacheLength=queueTCPCacheReceived. Count is greater than the actual queue length,

CodePudding user response:

We are now choosing pipeline pipe processing, if your.net version is low, then we suggest using BlockingCollection, it is suggested that

In your code, Dequeue is captured and removed, so you move out, he will of course length

CodePudding user response:

reference 4 floor wanghui0380 response:
we now choose pipeline pipe processing, if your.net version is low, so we suggest use the BlockingCollection, it is suggested that

In your code, Dequeue is captured and removed, so you move out, he will of course length
but I'll get it, the length of the moves from one by one again, should is just the right ah,

CodePudding user response:

reference 4 floor wanghui0380 response:
we now choose pipeline pipe processing, if your.net version is low, so we suggest use the BlockingCollection, it is suggested that

In your code, Dequeue is captured and removed, so you move out, he will of course length
and I found that if after the access to the count, sleep for a period of time, won't appear the phenomenon, but the delay increases a lot, doesn't meet my needs,

CodePudding user response:

Well,

Initial 9 on the

Now I remove a, 8 right now is

You fixed cycle nine times, ninth is empty, of course,
You said, actually cacheLength=queueTCPCacheReceived. Count is greater than the actual queue length, because the actual queue has been smaller than when you first came in

CodePudding user response:

refer to 6th floor maoxiao181018 response:
Quote: refer to 4th floor wanghui0380 response:
we now choose pipeline pipe processing, if your.net version is low, so we suggest use the BlockingCollection, it is suggested that

In your code, Dequeue is captured and removed, so you move out, he will of course length
and I found that if after the access to the count, sleep for a period of time, won't appear the phenomenon, but the delay increases a lot, doesn't meet my needs,


Oh, you are saying is the so-called timer, in the process of you sleep, you can't guarantee you the timer does not add

int cacheLength=queueTCPCacheReceived. Count;
Console. WriteLine (cacheLength. ToString ());
If (cacheLength & gt; 0)
{


See you lock in the order of the yao, you didn't lock him again, can only say that you are not by logic, but a coincidence, coincidentally, you sleep, the process of the timer running, by coincidence, not only run the timer, and he's really received the data

CodePudding user response:

If using a "queue" concept, you may be using 8, living in the last century s traditional style of the Unix system,

15 years history of the.net the practice of the "heavy"
 var x=ready to handle object; 
ThreadPool. QueueUserWorkItem (h=& gt; The asynchronous message processing (x));
, or the last 5 or 6 years of Task. Run (... ), is a more suitable the.net, because the net with a powerful system-level Task queue management mechanism, processing a variety of data and control problems, you don't have to, partial use many years ago based on the underlying programming code on some of the open source Java code, based on queue to write their own a Pool or Task management, it is easy to lost the wrong data

CodePudding user response:

You said I couldn't understand a word, ha ha,
  •  Tags:  
  • C#
  • Related