c# - How to fire event out of threading mode -



c# - How to fire event out of threading mode -

i used component socket connection in c#

i connect 10 tcp object server threading. , recived info in event (data_onrecived)

here code:

// connect function

private void connect(object state) { tcp tcp = (tcp)state; tcpsession tcpsession = new tcpsession(); tcpsession.remoteendpoint = new dart.sockets.ipendpoint(ip, convert.toint32(port)); tcpsession.connecttimeout = 1; seek { tcp.connect(tcpsession); } catch(system.net.sockets.socketexception ex) { tcp.marshal(ex); } }

// initial tcp component

private void tcpinitialize() { (int = 0; < maxconnection; i++) { socketcontrol[i].tcpconnection = new tcp(); socketcontrol[i].tcpconnection.data += new system.eventhandler<dart.sockets.dataeventargs>(this.tcp_data); socketcontrol[i].tcpconnection.start(connect, socketcontrol[i].tcpconnection); socketcontrol[i].tcpconnection.tag = i; } }

and tcp_data event handler fire when info recived:

void tcp_data(object sender, dart.sockets.dataeventargs e) { tcp tcp = (tcp) sender; int globalvariable = tcp.tag; }

my problem here tcp_data run in threading mode, not in queque , 1 one. can alter event dont start in threading mode?? mean every info recived, fire tcp_data after one.

thanks helping

you want execute 1 tcp_data callback @ time. create tcp_data perform synchronization. example, take lock:

object lockobj = new object(); void tcp_data(object sender, dart.sockets.dataeventargs e) { lock (lockobj) { tcp tcp = (tcp) sender; int globalvariable = tcp.tag; //... } }

you queue work taskscheduler single-threaded.

c# multithreading events

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -