c# - how to queue or buffer a series of commands in thread safe manner? -


i sending several command messages through udp port. how can queue them in background safe thread while program continues service..

the commands want send this:

 globaludp.udpclient.send(ipcmd, ipcmd.length, new system.net.ipendpoint(system.net.ipaddress.parse("255.255.255.255"), 30303));   globaludp.udpclient.send(ipcmd2, ipcmd.length, new system.net.ipendpoint(system.net.ipaddress.parse("255.255.255.255"), 30303));  globaludp.udpclient.send(ipcmd3, ipcmd.length, new system.net.ipendpoint(system.net.ipaddress.parse("255.255.255.255"), 30303)); 

any appreciated...

i suggest use blockingcollection<t>

the program's main thread can use blockingcollection.add() add items queue.

then background thread use getconsumingenumerable() output data, (where queue blockingcollection):

foreach (var item in queue.getconsumingenumerable()) {     // process item using globaludp.udpclient.send() } 

when main thread has finished enqueuing items , want background thread exit, call blockingcollection.completeadding() , terminate foreach being done background thread.

you write wrapper class data needed globaludp.udpclient.send() , use element type of blockingcollection.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -