Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class Client {
  2. public event EventHandler<ReceivedFileEventArgs> ReceivedFile;
  3.  
  4. private ReceivedFileEventArgs onReceivedFile() {
  5. EventHandler<ReceivedFileEventArgs> handler = ReceivedFile;
  6. ReceivedFileEventArgs args = new ReceivedFileEventArgs();
  7. if (handler != null) { handler(this, args); }
  8. return args;
  9. }
  10.  
  11. private void receiveFileCode() {
  12. // this is where you download the file
  13. ReceivedFileEventArgs args = onReceivedFile();
  14. if (args.Cancel) { return; }
  15. string filename = args.FileName;
  16. // write to filename
  17. }
  18. }
  19.  
  20. public class ReceivedFileEventArgs {
  21. public string FileName { get; set; }
  22. public bool Cancel { get; set; }
  23. }
  24.  
  25. public class MyForm : Form {
  26. public MyForm() { Initialize(); }
  27.  
  28. protected void buttonClick(object sender, EventArgs e) {
  29. // Suppose we initialize a client on a click of a button
  30. Client client = new Client();
  31. // note: don't use () on the function here
  32. client.ReceivedFile += onReceivedFile;
  33. client.Connect();
  34. }
  35.  
  36. private void onReceivedFile(object sender, ReceivedFileEventArgs args) {
  37. if (InvokeRequired) {
  38. // we need to make sure we are on the GUI thread
  39. Invoke(new Action<object, args>(onReceivedFile), sender, args);
  40. return;
  41. }
  42. // we are in the GUI thread, so we can show the SaveFileDialog
  43. using (SaveFileDialog dialog = new SaveFileDialog()) {
  44. args.FileName = dialog.FileName;
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement