magicRoot Posted January 28, 2009 Posted January 28, 2009 (edited) Hi, I programming a windows forms application with visualC++ 2008 Express Edition. I have a button event that triggers a folderBrowseDialog component to open. When the user chooses a folder, the dialog closes and the application hangs indefinitely. Heres the code button2_Click(System::Object^ sender, System::EventArgs^ e) { System::Threading::Thread^ temp = gcnew System::Threading::Thread( gcnew System::Threading::ThreadStart(this, &FormApp::Form1::folderDialog)); temp->SetApartmentState(System::Threading::ApartmentState::STA); temp->Start(); temp->Join(); } private: void folderDialog(){ ////create and show a directory dialog box folderBrowserDialog1 = gcnew FolderBrowserDialog(); //folderBrowser->RootFolder = Environment::SpecialFolder::MyComputer; //// save the current directory String^ currentDirectory = System::IO::Directory::GetCurrentDirectory(); ////show the dialog System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog(); if(result == System::Windows::Forms::DialogResult::OK){ folderName = folderBrowserDialog1->SelectedPath; //enable the preview button button3->Enabled = true; } } When I comment out temp->Join(), the app does not hang but if I do that, the folderBrowseDialog can be pushed behind the main application, which is what I dont want. I want the folderBrowseDialog to be in front until the user chooses a folder or exits the dialog. How can I stop the application hang? Thanks Edited January 28, 2009 by magicRoot Quote
stumper66 Posted January 28, 2009 Posted January 28, 2009 I don't know c++ at all, but have a few questions. Why are you making button1 create a new thread? Why not just call folderDialog() directly from button1? I suspect the reason the FBD shows behind the form is because it's being called from a non-UI thread (illegal cross-threading). If you need to have the code execute from a background thread, then use delegates to display the FBD. Quote
magicRoot Posted January 28, 2009 Author Posted January 28, 2009 calling showDialog() within button2_Click() brings up other issues; primarily, the folderBrowserDialog does not display properly, the filesystem tree is missing and all that is displayed in the dialog are buttons. I have read in other forums about that issue but no solutions given have worked in my code. This is the what I have resulted to so far. How would I go about using delegates? I have not worked with that mechanism much and would it solve my problem stated above? Thanks Quote
magicRoot Posted January 28, 2009 Author Posted January 28, 2009 I solved problem, here it is in case some of you run into the same bump button2_Click(System::Object^ sender, System::EventArgs^ e) { // save the current directory String^ currentDirectory = System::IO::Directory::GetCurrentDirectory(); //create a new thread that will display the foloderBrowserDialog // this is done to give the folderBrowserDialog freedom System::Threading::Thread^ temp = gcnew System::Threading::Thread( gcnew System::Threading::ThreadStart(this, &App::Form1::folderDialog)); temp->SetApartmentState(System::Threading::ApartmentState::STA); temp->Start(); temp->Join(); if(folderBrowserDialog1->SelectedPath != ""){ folderName = folderBrowserDialog1->SelectedPath; } // switch back to the working directory System::IO::Directory::SetCurrentDirectory(currentDirectory); } // thread function for the folder browser private: void folderDialog(){ //configure the dialog folderBrowserDialog1->SelectedPath = ""; //show the dialog System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog(); } 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.