Guest User

Untitled

a guest
Jan 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public void ReadAsync<T>(AsyncReadCallback<T> callback, AsyncException exception = null) {
  2. try {
  3. Thread thread = new Thread(new ThreadStart(() => {
  4. try {
  5. T request = ReadSync<T>();
  6. callback.Invoke(ref request);
  7. } catch(Exception e) {
  8. if(exception == null) {
  9. throw e;
  10. }
  11. exception.Invoke(e);
  12. }
  13. }));
  14. thread.Start();
  15. } catch (Exception e) {
  16. if(exception == null) {
  17. throw e;
  18. }
  19. exception.Invoke(e);
  20. }
  21. }
  22.  
  23. stream.ReadAsync((string result) => {
  24. Console.WriteLine("receive: "+result);
  25. },(Exception e) => {
  26. Console.WriteLine("exception: "+e.Message);
  27. });
  28.  
  29. public async Task<T> ReadAsync<T>() {
  30. return ReadSync<T>();
  31. }
  32.  
  33. Task<string> taskRead = stream.ReadAsync<string>();
  34. ...
  35. string data = await taskRead;
  36. Console.WriteLine(data);
Add Comment
Please, Sign In to add comment