Event notification between C # multithreading

Source: Internet
Author: User
Tags set time knowledge base

I have two threads,
Thread 1 accepts network data, and is stored in a queue;
Thread 2 takes the queue, carries out a variety of complex processing and then draws to the interface;
Want thread 1 to have data to notify thread 2, thread 2 to fetch the queue, because without notice, thread 2 has been in while loop retrieve queue when there is something, very cpu, how to get?
I now use the C # ManualResetEvent, thread 1 to the queue to add data, set to have a signal,
thread 2, the queue is empty for no signal, so the problem is to put a lot of signal, good waste
also want to have AutoResetEvent, I'm afraid two times the set activation event interval is too short to be problematic ...
What to do????, do not loop check but wait until 1 notice more 0 shared to: Related Knowledge Base: c#  virtual Reality (VR)  Node.js  algorithms and data structures   related recommendations:
  • c#.net_ Object-oriented programming technology
  • Public Platform Deepin Development Java Edition V2.0 (first quarter) Full version
  • developing information management systems with C #
  • Android Development Course "Java Core knowledge"
< Ins id= "Aswift_2_expand" >
  •  
  •  
Useful to me [0] throw a brick [0] reference | Report | Management number of replies: Ten
concern
wyd1520
Pull Light
This edition level:
 
#1 score: £ 2014-03-10 22:42:37
Please Baidu producers and consumers ... You'll get what you want.
Useful to me [0] throw a brick [0] reference | Report | Management
concern
wyd1520
Pull Light
This edition level:
 
#2 Score: 0 replies.: 2014-03-10 22:44:05 AutoResetEvent It's okay to use this.
Even if you're two times set, you don't have to go into the WaitOne () block until the last time you've done it.
Useful to me [0] throw a brick [0] reference | Report | Management
concern
lifang123456
I love everyone, 369
.
This edition level:
#3 Score: 0 replies.: 2014-03-10 22:56:36 Reference to the 2 floor wyd1520 's reply:
AutoResetEvent with this no problem,
Even if you're two times set, you don't have to go into the WaitOne () block until the last time you've done it.

Auto reset event, two set time interval is very small, the other thread waitone to continue once, that's not right,
Thread 1:
Have data 1-> join Queue->autoresetevent.set ()
Have data 2-> join Queue->autoresetevent.set ()
Thread 2:
while (true)
{
Autoresetevent.waitone ();
Fetch queue
.... Take other time-consuming actions
}
Thread 2 loop once, take a queue, I want to take two times, or else, just let the thread 2
Such
Whiile (True)
{----------------> This consumes CPU, or it can preempt other thread CPUs
Object obj = Queue.deueue ();//Take header element
if (null = = obj)
{
.... Take other time-consuming actions
}
}
?
Useful to me [0] throw a brick [0] reference | Report | Management
concern
wyd1520
Pull Light
This edition level:
 
#4 Score: £ 2014-03-10 23:05:21

You got it wrong.
To do this.

Thread 1 has data call Xxxenqueue

private void Xxxenqueue (XXX) into the queue
{
Lock (Queue)
{
Queue. Enqueue (XXX);

}
Auto. Set ();
}


Private XXX Xxxdequeue ()//Take head element
{
Lock (Queue)
{
return queue. Dequeue ();
}
}
Thread 2

Whiile (Auto. WaitOne ()) This is the
{
while (queue. COUNT&GT;0)
{
Object obj = Xxxdequeue ();
if (null = = obj)
{
.... Take other time-consuming actions
}
}
}
Useful to me [0] throw a brick [0] reference | Report | Management
concern
lifang123456
I love everyone, 369
.
This edition level:
#5 Score: 0 replies.: 2014-03-10 23:29:17 when is reset ()? There are also multiple threads that can be auto. Set ()? It's not locked.
Useful to me [0] throw a brick [0] reference | Report | Management
concern
lifang123456
I love everyone, 369
.
This edition level:
#6 Score: 0 replies.: 2014-03-10 23:30:05
When is reset ()? There are also multiple threads that can auto. Set ()? It's not locked.
Useful to me [0] throw a brick [0] reference | Report | Management
concern
wyd1520
Pull Light
This edition level:
 
#7 Score: 0 replies.: 2014-03-10 23:46:34 you go to reset () What he does, no reset ();
You can of course auto between multiple threads. Set (), you need to understand the producers and consumers. Relationship. Instead of a brain on set reset
Useful to me [0] throw a brick [0] reference | Report | Management
concern
lifang123456
I love everyone, 369
.
This edition level:
#8 Score: 0 replies.: 2014-03-11 13:19:19 I'm talking about a manual event, reset;
You don't understand my original question ...

If this is the case with automatic events (problematic):
Thread1 join the data1 when the queue, set triggered event Thread2 will WaitOne after the move immediately into a no signal,
After the data2 is added to the queue, the set triggers the event after the thread2 will turn the WaitOne and immediately turn into no signal.
If the set interval of two times in Thread1 is very small, thread2 will only pass the WaitOne once and no longer reads the data ...
Useful to me [0] throw a brick [0] reference | Report | Management

concern
lifang123456
I love everyone, 369
.
This edition level:
#9 Score: 0 replies.: 2014-03-11 22:00:07 to solve with Monitor.pulse!

Event notification between C # multithreading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.