Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by magicRoot
Posted

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.

Posted
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
Posted

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();
	 }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...