Open file i delegate

Open file i delegate
XA
  • Rejestracja:ponad 13 lat
  • Ostatnio:12 miesięcy
  • Postów:65
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?

Kopiuj
 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)

Kopiuj
 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 !
            }
        }
katelx
  • Rejestracja:około 10 lat
  • Ostatnio:6 miesięcy
  • Lokalizacja:Hong Kong
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

ŁF
Moderator
  • Rejestracja:ponad 22 lata
  • Ostatnio:dzień
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?


Azarien
  • Rejestracja:ponad 21 lat
  • Ostatnio:30 minut
0

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

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

wystarczy tak:

Kopiuj
this.BeginInvoke(_openFileDelegate, s);

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

Kopiuj
await OpenFile(s);

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.