asynccallback

How to access name of file within fs callback methods?

烂漫一生 提交于 2019-12-07 05:58:00
问题 How do I get access to the the arguments of fs.read,fs.stat... methods from within a callback? For instance if I want to process a file based on its size Following (coffeeScript) code snippet #assuming test1.txt exists filename = "./test1.txt" fs.stat filename, (err, stats) -> data = filename:filename,size:stats.size console.log data #further process filename based on size filename = "./test2.txt" prints { filename: './test2.txt', size: 5 } as filename is set to "./test2.txt". If I process

When should I use UdpClient.BeginReceive? When should I use UdpClient.Receive on a background thread?

吃可爱长大的小学妹 提交于 2019-12-06 02:07:01
Essentially, what are the differences between these beyond the obvious? When should I use which form? class What { public Go() { Thread thread = new Thread(new ThreadStart(Go2)); thread.Background = true; thread.Start(); } private Go2() { using UdpClient client = new UdpClient(blabla) { while (stuff) { client.Receive(guh); DoStuff(guh); } } } } versus class Whut { UdpClient client; public Go() { client = new UdpClient(blabla); client.BeginReceive(guh, new AsyncCallback(Go2), null); } private Go2(IAsyncResult ar) { client.EndReceive(guh, ar); DoStuff(guh); if (stuff) client.BeginReceive(guh,

How to implement reusable callback functions

点点圈 提交于 2019-12-05 23:47:47
问题 I am fairly new to JavaScript and I am working in node which requires a good understanding of async programming and callback design. I have found that using embedded functions is very easy to do even when your callbacks are multiple levels deep. Your embedded callbacks just end up being closures. However, when you have several layers of callbacks where many of the callbacks are similar across execute routes, you end up rewriting a lot of callback code over and over in separate callback chains

Async TCP Server for multiple Clients

强颜欢笑 提交于 2019-12-05 14:30:50
I've got an TCP Server that listens asynchronously for incoming connections. Everything works fine if just one client is connected. But if there are two or more connections, the Server doesn't get the first message. When i debug the ReceiveCallback function i can see that the Server gets the length of the message but not the data. I.e. if I connect two clients and try to send the first message: "hello", the server gets: received = 5; buffer= /0/0/0/0/0, so nothing is displayed. In the second message of the same client, the server gets the data. that's how my server looks like: private void

How to access name of file within fs callback methods?

允我心安 提交于 2019-12-05 11:31:08
How do I get access to the the arguments of fs.read , fs.stat ... methods from within a callback? For instance if I want to process a file based on its size Following (coffeeScript) code snippet #assuming test1.txt exists filename = "./test1.txt" fs.stat filename, (err, stats) -> data = filename:filename,size:stats.size console.log data #further process filename based on size filename = "./test2.txt" prints { filename: './test2.txt', size: 5 } as filename is set to "./test2.txt". If I process/read the file using filename variable within fs.stat callback it would use test2.txt which is not

Pass argument to AsyncCallback function?

我的未来我决定 提交于 2019-12-05 01:27:37
I'm learning socket programming and I have the following function: public void OnDataReceived(IAsyncResult asyn) and this is how the callback gets set: pfnWorkerCallBack = new AsyncCallback(OnDataReceived); The problem is I need to pass another argument to OnDataReceived callback function, how can I do this? I'm trying to make a simple tcp server and I need to track from which client the data is coming from. Any tips? Thanks! I'm going to presume you're using System.Net.Sockets.Socket here. If you look at the overloads of BeginReceive you'll see the object parameter (named state). You can pass

C# How do I pass more than just IAsyncResult into AsyncCallback?

十年热恋 提交于 2019-12-05 00:42:29
问题 How do I pass more than just the IAsyncResult into AsyncCallback? Example code: //Usage var req = (HttpWebRequest)iAreq; req.BeginGetResponse(new AsyncCallback(iEndGetResponse), req); //Method private void iEndGetResponse(IAsyncResult iA, bool iWantInToo) { /*...*/ } I would like to pass in example variable bool iWantInToo . I don't know how to add that to new AsyncCallback(iEndGetResponse) . 回答1: You have to use the object state to pass it in. Right now, you're passing in the req parameter -

How to implement reusable callback functions

我的梦境 提交于 2019-12-04 04:11:51
I am fairly new to JavaScript and I am working in node which requires a good understanding of async programming and callback design. I have found that using embedded functions is very easy to do even when your callbacks are multiple levels deep. Your embedded callbacks just end up being closures. However, when you have several layers of callbacks where many of the callbacks are similar across execute routes, you end up rewriting a lot of callback code over and over in separate callback chains. For example, if mycb1 and mycb2 definitions below are moved outside of A, they no longer have

C# How do I pass more than just IAsyncResult into AsyncCallback?

本小妞迷上赌 提交于 2019-12-03 15:48:10
How do I pass more than just the IAsyncResult into AsyncCallback? Example code: //Usage var req = (HttpWebRequest)iAreq; req.BeginGetResponse(new AsyncCallback(iEndGetResponse), req); //Method private void iEndGetResponse(IAsyncResult iA, bool iWantInToo) { /*...*/ } I would like to pass in example variable bool iWantInToo . I don't know how to add that to new AsyncCallback(iEndGetResponse) . You have to use the object state to pass it in. Right now, you're passing in the req parameter - but you can, instead, pass in an object containing both it and the boolean value. For example (using .NET 4

C# Socket.Receive message length

萝らか妹 提交于 2019-12-03 08:19:47
I'm currently in the process of developing a C# Socket server that can accept multiple connections from multiple client computers. The objective of the server is to allow clients to "subscribe" and "un-subscribe" from server events. So far I've taken a jolly good look over here: http://msdn.microsoft.com/en-us/library/5w7b7x5f(v=VS.100).aspx and http://msdn.microsoft.com/en-us/library/fx6588te.aspx for ideas. All the messages I send are encrypted, so I take the string message that I wish to send, convert it into a byte[] array and then encrypt the data before pre-pending the message length to