Kirk_G Posted February 22, 2004 Posted February 22, 2004 I have a MDI application in C# that loads image data in MDI windows. The child form has a constructor with a filename parameter for the image data. If I try to open large files or files over a network it can be slow and halt the parent program. Is there a way to asyncronously load MDI windows? I have tried creating a delegate with the child forms signature, calling the delegates BeginInvoke method with a callback method that sets the child forms parent to "this" but there seems to be some fundamental problem with this idea. Everything is great up to the point where I try to set the MDIparent parameter to "this". Here is a snipet: public delegate ChildForm Async_ChildFormDelegate(string filename, bool logdata); //Now inside of ParentForm class public void LoadUnloggedData() { OpenFileDialog mydialog = new OpenFileDialog(); mydialog.Title = @"Open Un-Logged Images"; mydialog.Multiselect = true; if(mydialog.ShowDialog() == DialogResult.OK) { foreach (String name in mydialog.FileNames) { Async_ChildFormDelegate AsyncLoadOCT = new Async_ChildFormDelegate(Async_LoadData); IAsyncResult asyncResult = AsyncLoadOCT.BeginInvoke(name,true,new AsyncCallback(CallbackHandler),AsyncLoadOCT); } } } public ChildForm Async_LoadData(string filename, bool logdata) { ChildForm newOCT = new ChildForm(filename,logdata); return newOCT; } public void CallbackHandler(IAsyncResult result) { Async_ChildFormDelegate childForm = (Async_ChildFormDelegate)result.AsyncState; ChildForm newOCT = childForm.EndInvoke(result); newOCT.MdiParent = this; // Set Cursor Size newOCT.Xsize = sizeX; newOCT.Ysize = sizeY; // Display the new form. newOCT.Show(); } Everything is fine until newOCT.MdiParent = this; at which point the program jumps back up the top of the callbackhandler method, reruns the first 2 lines and leaves the method. The parent window come back up with no MDIchild loaded. Any ideas? Thanks Kirk Quote
Kirk_G Posted February 23, 2004 Author Posted February 23, 2004 Here is an updated code snipet from a simpler example I created just to test this point public delegate childForm AsyncLoadMDIDelegate(string filename); private void openItem_Click(object sender, System.EventArgs e) { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Title = @"Select Image Files"; openDialog.Multiselect = true; if(openDialog.ShowDialog() == DialogResult.OK) { foreach(string name in openDialog.FileNames) { //This commented out section works just fine //childForm newChild = new childForm(name); //newChild.MdiParent = this; //newChild.Show(); //Not sure why this doesn't work AsyncLoadMDIDelegate loadMDIDelegate = new AsyncLoadMDIDelegate(this.loadMDI); IAsyncResult asyncResult = loadMDIDelegate.BeginInvoke(name,new AsyncCallback(this.CallBackHandler),loadMDIDelegate); } } } public void CallBackHandler(IAsyncResult result) { AsyncLoadMDIDelegate loadMDIDelegate = (AsyncLoadMDIDelegate)result.AsyncState; childForm newChild = loadMDIDelegate.EndInvoke(result); newChild.MdiParent = this; //This is where the program doesn't work newChild.Show(); } public childForm loadMDI(string filename) { childForm newChild = new childForm(filename); return newChild; } Can anyone tell me why this doesn't asyncronously load MDI wondows. I don't get any exceptions, they just don't show up. The newChild.Show(); never runs because it never gets past the newChild.MdiParent = this; never works? Any help would be appriciated. Thanks Kirk Quote
Kirk_G Posted February 24, 2004 Author Posted February 24, 2004 Can anyone tell me if I'm missing something obvious here or is theresome fundamental reason this can't be done? Thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.