Solved

C# .NET Anomaly?

Posted on 2012-04-06
10
350 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:SteveDXL
Comment Utility
@ 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
Comment Utility
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
Comment Utility
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
Comment Utility
Great. Working now! Thanks much for your help sir!
0
 
LVL 13

Expert Comment

by:Ashok
Comment Utility
Glad to help

Thank you
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now