Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# .NET Anomaly?

Posted on 2012-04-06
10
354 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 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