0

I have windows application, where my program have the following code:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

Now, on the MainForm(), I have few buttons, where upon clicking each of the button, I hide the MainForm and open a new form (Windows Form) using opendialog as shown in the below code:

this.Hide();
TestCenter testCenter = new TestCenter();
testCenter.ShowDialog();
this.Show();

Now, in the TestCenter form, I have a functionality (OpenFileDialog) for selecting a file, as shown in the below code:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "image file |*.jpg;*.png;*.gif";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Cancel)
    return;
pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
txt_ImagePath.Text = ofd.FileName;

I have a TextBox and a PictureBox, for showing the filepath and the image after making the selection in OpenFileDialog.

The weird thing is, that, when I run this program from Visual Studio or from the installed programs on my laptop (Windows 10) it is working excellently without any issue. But when I install this on client machine (Windows 7) it is freezing this Windows Form application when I click on the button which calls this OpenFileDialog().

Can someone please help me with this issue?

--------EDIT--------2/7/18--------

private void btnImage_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    // ofd.ShowHelp = true;
    ofd.Filter = "Image Files (*.png, *.gif, *.jpg)|*.png; *.gif*;*.jpg";
    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.Cancel)
        return;
    pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
    txt_ImagePath.Text = ofd.FileName;
}
Raju
  • 1
  • 6
  • How about using task manager to create a mini dump an run this in Visual Studio? – Uwe Keim Feb 04 '18 at 04:37
  • @Uwe Keim, Pardon me, I am new to Visual Studio and C# and this is my first Windows Forms application. – Raju Feb 04 '18 at 04:43
  • I didn't understand your comment, "using task manager to create a mini dump and run this in Visual Studio", how do I do this and what do I check? – Raju Feb 04 '18 at 04:45
  • Anybody...please... – Raju Feb 05 '18 at 02:30
  • How long did you wait? Maybe the users image file is larger/PC slower? Maybe it's one of them odd Image.FromFile locking issues. – user6144226 Feb 05 '18 at 15:47
  • Try it on another machine. The client might have some explorer add-ons that could freeze the dialog. – LarsTech Feb 05 '18 at 16:38

3 Answers3

0

I don't think your code is the problem. I suspect it is an installation issue. Make sure this program works first:

using System;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.ShowDialog();
    }
}

Make sure you have installed the correct version of the .NET runtime that you are targetting.

The OpenFileDialog will load shell extensions into your application, if the client machine has strange shell extensions installed then it could interfere with your application.

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • will try and execute this and see how the client machine responds. and regarding the shell extensions, I know very little about them, can you plz elaborate on that? fyi... the client machine is a clean desktop and we had the latest .NET runtime. – Raju Feb 06 '18 at 03:24
  • The program you have given works perfectly fine on the client machine. Also, I have tried creating couple of forms (MainForm, Form1). And loaded the MainForm when application started and there I gave a button to open the Form1 using ShowDialog and in the Form1, I placed a button, textbox and picturebox. when I click the button, I am opening the OpenFileDialog and looking for image files, once selected, I am populating the textbox and picturebox, with filepath and image. This is also perfectly working fine on the client machine. – Raju Feb 06 '18 at 05:52
  • But, my windows application still doesn't work. Will try to recreate my scenario in this sample program sans all the other functionality, to see if this sample program still works. – Raju Feb 06 '18 at 06:37
  • If the test program I provided here works fine on the client, then the OpenFileDialog is not the problem. You have issues elsewhere in your code. You'll need to do some more detective work. How do you observe that "it is freezing this Windows Form application when I click on the button"? Maybe you should reveal the code for your button click handler. – Wyck Feb 06 '18 at 16:49
  • I too wonder the same, if the sample code works, then why not my code. I am trying to do some investigation (trail n error) to see if I can get by this freaky issue. I have already pasted all of my code in the button click event in my question Will edit my question and add the method signature as well. – Raju Feb 06 '18 at 21:42
  • regarding freezing of the application, when I click on the button, it opens up the OpenFileDialog (just the window) and doesn't load any of it's contents and when I click any part of the window or app (either immediately or after a while) the app fades out opening up a small window with options to debug/find a solution/close the program. – Raju Feb 06 '18 at 22:37
  • what dlls are loaded for you? (Debug > Windows > Modules; Ctrl+Alt+U) For me: mscorlib.dll, WindowsFormsApp1.exe, System.Windows.Forms.dll, System.dll, System.Drawing.dll, System.Configuration.dll, System.Core.dll, System.Xml.dll – Wyck Feb 07 '18 at 05:04
  • May I suggest creating a clean Windows 7 VM and trying again? I'm worried something bogus is installed on the Client Windows 7 machine. Something like a shell extension. Also makes me wonder if you should try [ShellExView](https://www.nirsoft.net/utils/shexview.html) or something similar to see if there's a windows explorer extension messing things up. – Wyck Feb 07 '18 at 05:12
  • I tried installing VS2017 and debug the app on client machine. It is giving me "System.AccessViolationException". It says, "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Any idea how to resolve this. I tried creating a new folder for picking up the images and set the permissions to full control on this folder and to the directory that I created this folder in. And in my code I set the initial directory to this newly created one, but still the app hangs by throwing the above exception. Any Ideas on how to resolve this ??? – Raju Feb 08 '18 at 07:37
  • That's great that you have access to the client machine and got VS2017 running on it. You could potentially get all your project files over there too and build a debug version that is fully ready for debugging. Focus on trying to determine the call stack, In other words, run your app from the debugger, and it should catch the AccessViolationException and show you the line of code that was executing that caused the exception. That will be your biggest clue as to what is happening. – Wyck Feb 08 '18 at 14:57
  • I have already done that, and the exception is coming at "DialogResult dr = ofd.ShowDialog();". I asked for a clean install of windows 7 to be done tomorrow, without any other user installed programs, once I get that system, I will check and see if this exception is gone or not. And my only doubt is still not cleared, why this exception is occurring at the specified line of code in my program and not happening in the sample program!! – Raju Feb 08 '18 at 16:08
  • Just so I'm 100% clear. In *your* code, `DialogResult dr = ofd.ShowDialog()` causes an AccessViolationException, but in my suggested code `OpenFileDialog dlg = new OpenFileDialog(); dlg.ShowDialog();` works fine, right? To me the only thing different is the Filter. Could you try deleting the Filter? (leaving it as default) – Wyck Feb 08 '18 at 16:27
  • yes Wyck, whatever you understood is correct. I will try out few more things tomorrow (my client is not available now) and will update my findings here. – Raju Feb 08 '18 at 17:09
0

Debug the program on the Windows 7 machine to narrow to exact issue.

  • Compile the program with debug symbols
  • Install remote debugging on the Windows 7 machine
  • Start the your program on the Windows 7 machine
  • Use Visual Studio on your developer machine to connect to the Windows 7 machine
  • Put a break point on the button to halt the code
  • Debug Interactively to see what call exactly is hanging

    Troubleshoot further from there, you'll have more information. I've seen this before when I was building for ANYCPU. Try x86 for testing.

Billy Willoughby
  • 806
  • 11
  • 15
  • sorry, I am not that kind of a hardcore programmer and knows not much on debugging techniques. – Raju Feb 06 '18 at 03:20
  • It's not too complex. If you follow the steps above and break it down to single tasks, I'm sure you can get it done if you like. Microsoft has a walk through: https://msdn.microsoft.com/en-us/library/y7f5zaaa.aspx – Billy Willoughby Feb 06 '18 at 18:31
0

Apparently, adding OLE DB Services = -1 to my Connection String solved the issue.

In my form, I am using Access DB Connection for fetching data from the database. And this is the one that is messing up with loading of OpenFileDialog. And, this also explains why the sample code (by Wyck) is working fine (since there was no usage of DB Connection in there).

And I'm wondering why the answer by Vikas4u for this question (which was my reference) was voted down.

Raju
  • 1
  • 6