Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Statement Not Executed After Return From Function 1

Status
Not open for further replies.

patrickc3000

Programmer
Aug 13, 2008
2
US
I have some code that basically copies from one directory into another, hides a winform, and shows the next form in the wizard. Sometimes it fails to execute the hide function, but if I physcially move the form, the next form is behind it. If I attempt to perform the same event a second time, it will behave as desired. Here is the code:

If I put a statement that I don't care if it gets executed or not after the return from the called function, like "string waitString = "wasting time";" it will execute the this.hide() statement every time. But, that is ugly and I want to understand what is going on.

private void btnNext_Click(object sender, EventArgs e)
{
if (!_destinationFolder.Contains(_catalogName))
_destinationFolder += "\\" + _catalogName;

//Check to see if the directory exists
if (Program.CheckDirectory(_destinationFolder))
{
//The catalog already exists in the directory, ask the user if they wish to overwrite it.

DialogResult result = MessageBox.Show("Do you want to overwrite the existing catalog folder in " + Program.IcatDataPath + "?","Overwrite", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

if (result == DialogResult.Cancel)
{
//Do not overwrite
return;
}
else
{
//overwrite
Cursor.Current = Cursors.WaitCursor;
Program.CopyDirectory(_selectedFolder, _destinationFolder);

//pause for processing
System.Threading.Thread.Sleep(5000);
Cursor.Current = Cursors.Default;


//optional code that will make sure this.Hide() gets called.

string waitString = "wasting time";
[/color green]

//This is the code that doesn’t get executed sometimes. [/color red]

this.Hide();

//This code always gets executed.[/color red]
_genesisPropertiesForm.Activate();
_genesisPropertiesForm.Show();
}
}


 
Not sure if it will work. Try this:

Application.DoEvents();
//This is the code that doesn't get executed sometimes.
 
It seems that you may need a background process or use Multithreading. However, Application.DoEvents(); worked for me in a similar condition; although, I am not exactly sure HOW it works completely.

 
Yeah you're probably right. I will probably end up multi-threading this task. I use a background worker in other spots in the application. I just took the easy way out with this task and changed the cursor to a waitcursor while the directory was copying. I figured between that and sleeping the thread, I was covered and it would have no problems operating in a linear fashion. The UI has no need to be responsive while this task is completing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top