Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Client {
- public event EventHandler<ReceivedFileEventArgs> ReceivedFile;
- private ReceivedFileEventArgs onReceivedFile() {
- EventHandler<ReceivedFileEventArgs> handler = ReceivedFile;
- ReceivedFileEventArgs args = new ReceivedFileEventArgs();
- if (handler != null) { handler(this, args); }
- return args;
- }
- private void receiveFileCode() {
- // this is where you download the file
- ReceivedFileEventArgs args = onReceivedFile();
- if (args.Cancel) { return; }
- string filename = args.FileName;
- // write to filename
- }
- }
- public class ReceivedFileEventArgs {
- public string FileName { get; set; }
- public bool Cancel { get; set; }
- }
- public class MyForm : Form {
- public MyForm() { Initialize(); }
- protected void buttonClick(object sender, EventArgs e) {
- // Suppose we initialize a client on a click of a button
- Client client = new Client();
- // note: don't use () on the function here
- client.ReceivedFile += onReceivedFile;
- client.Connect();
- }
- private void onReceivedFile(object sender, ReceivedFileEventArgs args) {
- if (InvokeRequired) {
- // we need to make sure we are on the GUI thread
- Invoke(new Action<object, args>(onReceivedFile), sender, args);
- return;
- }
- // we are in the GUI thread, so we can show the SaveFileDialog
- using (SaveFileDialog dialog = new SaveFileDialog()) {
- args.FileName = dialog.FileName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement