Solved

C# .NET Anomaly?

Posted on 2012-04-06
10
351 Views
Last Modified: 2012-06-27
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...

code seems to be executing normally.
I step into the if...

file is definitely there...
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...

nothing happens...?
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:

the second messagebox shows, but not the first. what the heck?!
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?
0
Comment
Question by:SteveDXL
  • 6
  • 2
10 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 37818148
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
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37818179
Also

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

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

HTH
Ashok
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37818186
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
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37818193
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.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:SteveDXL
ID: 37818233
@ 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
0
 
LVL 86

Expert Comment

by:jkr
ID: 37818272
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.
0
 
LVL 13

Accepted Solution

by:
Ashok earned 500 total points
ID: 37818341
You will need to implement a static method for your Alert class if you want the exact MessageBox-like behaviour.

public static DialogResult Show(string text)
{
    Alert form = new Alert(text);
    return form.ShowDialog();
}

Now you can use the form by calling:

Alert.Show("my message");

HTH
Ashok
0
 

Author Comment

by:SteveDXL
ID: 37818430
Great. Working now! Thanks much for your help sir!
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37818975
Glad to help

Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now