Open file i delegate

0

Witam,
Chciałem sobie zrobić w mojej apce otwieranie plików poprzez np. metodę przeciągnięcia pliku na forma.
Tutaj jest super przykład:
http://snipplr.com/view/47126/

Zastanawia mnie (po prostu nie czuję tego i prosiłbym o wytłumaczenie) czemu musimy użyć delegacji?

 private void Form1_DragDrop(object sender, DragEventArgs e) {
			//good idea to use try-catch block, in case something goes wrong
			try {
                Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
                if (a != null) {
                    // Extract string from first array element
                    // (ignore all files except first if number of files are dropped).
                    string s = a.GetValue(0).ToString();
                    // Call OpenFile asynchronously.
                    // Explorer instance from which file is dropped is not responding
                    // the entire time that the DragDrop handler is active, so we need to return
                    // immidiately (especially if OpenFile shows MessageBox).
                    this.BeginInvoke(_openFileDelegate, new Object[] { s });
                    this.Activate();        // in the case Explorer overlaps this form
                }
            }
            catch (Exception ex) {
                Trace.WriteLine("Error in DragDrop function: " + ex.Message);
                // don't show MessageBox here - Explorer is waiting !
            }
        } 

Czemu nie można tego zrobić tak(bez użycia delegacji)

 private void Form1_DragDrop(object sender, DragEventArgs e) {
			//good idea to use try-catch block, in case something goes wrong
			try {
                Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
                if (a != null) {
                    // Extract string from first array element
                    // (ignore all files except first if number of files are dropped).
                    string s = a.GetValue(0).ToString();
                    // Call OpenFile asynchronously.
                    // Explorer instance from which file is dropped is not responding
                    // the entire time that the DragDrop handler is active, so we need to return
                    // immidiately (especially if OpenFile shows MessageBox).
					OpenFile(s); //ZAMIANA!!
                    this.Activate();        // in the case Explorer overlaps this form
                }
            }
            catch (Exception ex) {
                Trace.WriteLine("Error in DragDrop function: " + ex.Message);
                // don't show MessageBox here - Explorer is waiting !
            }
        }
0

gdy po prostu wczytasz plik zamiast zrobic to przy uzyciu BeginInvoke GUI zostanie zamrozone na czas wczytywania. masz to troche wyjasnione w tej stercie nadmiarowych komentarzy

0

Masz jak wół napisane: Explorer instance from which file is dropped is not responding the entire time that the DragDrop handler is active, so we need to return immidiately (especially if OpenFile shows MessageBox). Czego nie rozumiesz?

0

this.BeginInvoke(_openFileDelegate, new Object[] { s });

Dziwi mnie że wszystkie przykłady w necie utrudniają sobie z tym new Object.

wystarczy tak:

this.BeginInvoke(_openFileDelegate, s);

można też metodę uczynić asynchroniczną i użyć await:

await OpenFile(s);

1 użytkowników online, w tym zalogowanych: 0, gości: 1