Link to home
Start Free TrialLog in
Avatar of SteveDXL
SteveDXL

asked on

C# .NET Anomaly?

Hi experts.
I've encountered an extremely strange incident. I am implementing a very simple "cancel" function into a downloader, but I need to make sure that after the download of the file is cancelled, that the file that was being written to is deleted.

Here's the code:
        private void button1_Click(object sender, EventArgs e)
        {
            client.CancelAsync();
            while (!File.Exists(ToSave))
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }

            if (File.Exists(ToSave))
            {
                MessageBox.Show("File exists");
            }
            client.Dispose();
            Dispose();
            Close();
        }

Open in new window


Now it's clear that if the execution gets passed the while() loop, then the file has to be there. But just for debugging purposes, I'm checking if it's there anyway.

So I break on if the file exists...

User generated image
I step into the if...

User generated image
Now at this point, of course, C# thinks the file is there (well, because the file is there...)

Now I continue to step into the flow...

User generated image
But something very strange happens here.... NOTHING! No message box is shown. I don't understand why this is... I've never seen something like this.

So out of curiosity, and bewilderment, I throw in another check to see if the file is locked, and display a messagebox if it is not locked. Shown below:

User generated image
Now, the second messagebox shows but the first one does not. What's going on?!?!?!?!?!?!?

 It's as if there's something fundamentally wrong with making the call to MessageBox(), but only for that one block of code. The rest of the application runs perfectly with Messageboxes.


Have any of you had any similar issues?
Avatar of Ashok
Ashok
Flag of United States of America image

First try inserting one line after first MessageBox.....

Application.DoEvents();

If that does not work, then try following.....

Put the breakpoint after first MessageBox
on
if (!   LINE

then after it breaks

press ALT-TAB (many times to cycle thru all Windows)

to see if the first MessageBox is hidden or NOT.

HTH
Ashok
Also

try changing first MessageBox to use following format.....

MessageBox.Show(wizardModalReference, "File exists", "Details", MessageBoxButtons.OK, MessageBoxIcon.Information);

HTH
Ashok
One more trick....

Instead of using the standard MessageBox.Show, try making your own Form with 1 button for [ok]. Set topmost to true and then MyErrorForm.ShowDialog();

HTH
Ashok
MessageBox.Show(wizardModalReference, "File exists", "Details", MessageBoxButtons.OK, MessageBoxIcon.Information);

Change wizardModelReference to name of your form (parameter owner).

The messagebox.show has some overloads that specify the parameter owner. pass in there the main form, and that should do it.
Avatar of SteveDXL
SteveDXL

ASKER

@ ashok111,
using the MessageBox.Show(wizardModalReference, "File exists", "Details", MessageBoxButtons.OK, MessageBoxIcon.Information); did not work unfortunately. But I did try to create a new form to handle it individually, and that had better results, but still not functioning correctly.

Changed code to:
        private void button1_Click(object sender, EventArgs e)
        {
            client.CancelAsync();
            client.Dispose();
                var hc = new HandleCleanup(ToSave);
                hc.ShowDialog();
            Dispose();
            Close();
        }

Open in new window


I don't need those checks anymore because now I'm handling all of the logic on the new form. But now the form will appear, and then disappear instantly.

Any other ideas?


@jkr, sorry I know this is not directly related to C++, but I was under the impression I had to choose 3 topics, 1 being c#, another .NET and then for the last I went with c++ because I assumed that many c++ programmers have c# experience or at least have dealt with the general category of problem I'm having

sorry if I violated the rules with that misnomer
Well, you don't *have* to to pick three TAs, it is just an option ;o)

Will try to find something more suitable to move you to, but will remove the C++ Zone reference in the end, since it is not related at all. Thank you for your understanding.
ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great. Working now! Thanks much for your help sir!
Glad to help

Thank you