[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# .NET Anomaly?

Posted on 2012-04-06
10
Medium Priority
?
358 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
[X]
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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simple Linear Regression
Six Sigma Control Plans

649 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