Windows Forms GUI hangs when calling OpenFileDialog.ShowDialog()

my project a three tier architecture project talking to a WCF service in the backend. When the backend is able to fetch data from the service, it notifies the business layer using publish-subscribe, which in return notifies the GUI layer.

I have added an OpenFileDialog to my UI design using Visual Studios designer. A button event handler calls the ShowDialog message. However, once I click the button, the whole UI hangs.

Having googled around a bit, I found out that using delegates is the preferred way to handle tasks like this. However, with nor without delegate the problem persists.

Currently my code looks like this:

private void bOpen_Click(object sender, EventArgs e) { Func<Image> del = delegate { OpenFileDialog d = new OpenFileDialog(); if (d.ShowDialog() == DialogResult.OK) { return Image.FromFile(d.FileName); } return null; }; Invoke(del); }

I'm coming from the Java world, so I'm not really familiar with the intricacies of C# UI programming.

Anything I'm missing here?

-------------Problems Reply------------

I seem to have solved the problem adding the [STAThread] Attribute to the main method. I was told to do so once I ran the program in a debugger - which I hadn't done before because I ran the service from Visual Studio and the client regularly from Windows.

[STAThread]
public static void Main(string[] args)
{
GUI gui = new GUI();
gui.ShowDialog();
}

Can anybody explain what exactly is going on though

This is an environmental problem, when you use OpenFileDialog a lot of shell extensions get loaded into your process. A misbehaving one can easily screw up your program. There are a lot of bad ones out there.

Debugging this is difficult, you need an unmanaged debugger since these shell extensions are unmanaged code. You might be able to tell something from the call stack when you break in after the deadlock. Windows debugging symbols required, enable the Microsoft symbol server. But the most effective approach is to use SysInternals' AutoRuns utility. Start by disabling all of the shell extensions that were not produced by Microsoft. Then start re-enabling the ones you cannot live without one by one.

I believe the "delegate" prefered way actually refers to using a separate thread. I'm gonna give you an example using BackgroundWorker.

It would look like this:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_Worker.DoWork += new DoWorkEventHandler(m_Worker_DoWork);
m_Worker.ProgressChanged += new ProgressChangedEventHandler(m_Worker_ProgressChanged);
m_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_Worker_RunWorkerCompleted);
}

void m_Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Usually, used to update a progress bar
}

void m_Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Usually, used to add some code to notify the user that the job is done.
}

void m_Worker_DoWork(object sender, DoWorkEventArgs e)
{
//e.Argument.ToString() contains the path to the file
//Do what you want with the file returned.
}

private void bOpen_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == DialogResult.OK)
{
m_Worker.RunWorkerAsync(d.FileName);
}
}

BackgroundWorker m_Worker = new BackgroundWorker();
}

Now, as for the reason your UI "hangs", it's because by default, your operation runs on the UI thread, so if you run something heavy the UI won't respond.

Category:c# Views:8 Time:2011-07-16

Related post

  • When does Microsoft.Win32.OpenFileDialog.ShowDialog() return null? 2009-08-11

    OpenFileDialog's ShowDialog method returns a nullable boolean, set to true if the user clicked OK or false if he clicked Cancel. When does it return null? The documentation does not say. --------------Solutions------------- This is stated in the ques

  • Throwing error on OpenFileDialog.Showdialog in c#.net 2011-05-17

    I am developing a windows application in XP machine.in my one form i have one text box for showing the selected file with path from openfiledialog box.and the other control is button to call the openfiledialog box. When I cliked on the button to call

  • Why Ruby 1.9 GUI hangs if i do any intensive computation in separate Ruby thread? 2012-01-30

    Ruby 1.9 is supposed to have native threads, and GIL is supposed to lift if some threads enters native code (like GUI toolkit main loop or C implementation of some Ruby lib). But if i start following simple code sample that displays GUI in main threa

  • What could cause a Windows Service to hang when a Console App doing the exact same thing using the exact same base libraries doesn't? 2009-12-03

    I hate asking questions like this - they're so undefined... and undefinable, but here goes. Background: I've got a DLL that is the guts of an application that is a timed process. My timer receives a configuration for the interval at which it runs and

  • Automating interaction with a closed-sourced Windows XP GUI-based Program 2012-04-12

    I've a closed-sourced Windows XP GUI-based that performs some measurements. My current manual workflow is: Start the data capture by clicking on the "Run" button on the GUI Stop the data capture by pressing the "Stop" button on the GUI. Read some val

  • Windows phone device hangs / restarts 2012-05-03

    My windows phoned devise hangs / restarts quite often. I tied the application both in Samsung Focus and in my HTC device both responds in the same manner. The exact repo is , i tried for testing the application tombstone functionality. For that i con

  • windows install cleanup Hangs 2013-05-02

    Vista 64 When I run Windows Clearn Up Tool I specifiy a program (PowerDirector) to remove. Windowns Clean Up then opens a Cmd Window and runs a number of coomands to remove the program's components. Windows Cleanup then hangs up and I have to Exit ou

  • Windows XP Restart hangs - Fails to complete shutdown 2013-05-15

    Windows XP desktop hangs during a restart. The machine logs off the user account and displays "shutting down" but never finishes the shutdown. The problems only happens during a restart ("shutdown" and "log off" functions work fine). I just reinstall

  • Windows Live Mail - hangs when I try to enter in the Facebook's contacts 2012-01-12

    When I try to enter into contacts Windows Live Mail hangs and does not respond. He was already reinstal and nothing helped. Turn off anti-virus (Kaspersky 2012). Can anyone help me with this ... I would add that address gets Facebook accounts. ------

  • I am using Windows 8 and my Windows Live Mail hangs. 2012-02-13

    w8, windows live mail, hangs by receiving emails. after clicking finish, it never finishes the process. It happens with 3 email accounts set up, or with just 1. --------------Solutions------------- Hi Przemek71, I would like to try and help you with

  • Windows 8 update hangs system with "installing 3 of 5 updates" 2012-02-20

    From the recent upgrade to Win 8 Pro (from Win 7), when prompted recently to allow updates, the update process hangs on the "installing 3 of 5 updates - don't turn off system while update is in process" - but unable to do anything to free that up, an

  • I am having trouble updateding windows it just hangs and other issues. 2012-03-02

    Original title: Internet connectivity issue I am having trouble updateding windows it just hangs , and i noticed my wireless printer only prints the first page then say internet connectivity issue , i also seem to have problems with other softwear ,

  • Windows 8 Apps Hang when opening (Store, Music, Video, Travel etc.) 2012-03-21

    Hello, Every bundled Windows 8 app hangs when opening without an error message. Similar to this issue, every app opens, but shortly thereafter the logo shifts up to the top left corner and the rotating dot circle just goes and goes. I've tried every

  • Windows 7 install hangs in ACHI Mode 2012-03-22

    Windows 7 install hangs at 'Starting Windows' screen when in ACHI mode. Changing it to compatibility (IDE) allows the installation to continue to choosing the partition for installation. At that point it says it isn't possible because I'm not in ACHI

  • Windows 8 Store hangs 2012-04-24

    When I click on the Store app, it opens, but shortly thereafter the logo shifts up to the top left corner and gets smaller and the rotating dot circle just goes and goes. --------------Solutions------------- Hi, I understand that the Windows Store ap

  • Window 8 GUI for Windows Server 2008 and up GUI 2012-04-27

    I was curious with the rollout of Windows 8, Surface RT, Windows Mobile and JAVA patchs in systems and software, can..... The windows 8 GUI be used for future server software? If not, how long untill so? --------------Solutions------------- I'm not e

  • windows 8.1 hangs on startup. Log file: C:\WINDOWS\System32\logfiles\Srt\SrtTrail.txt 2012-04-30

    Split from: http://answers.microsoft.com/en-us/windows/forum/windows8_1-performance/run-dll-file-in-windows-81/b32f7c5e-985f-4e8d-add4-2364f5e241c9 My laptop Lenovo G580 with windows 8.1 hangs on startup. i tried Startuo repair but following error ap

  • Windows 7 Momentarily Hangs/Freezes every 10 seconds or so for 1/2 secs including the mouse. 2012-05-09

    Previously the dell system has W7 32bit installed but would continually shutdown after a few hours use. I was guessing it may be related to 32bit Windows so decided to setup W7 x64 ... The system doesnt crash/shutdown anymore but now am getting Momen

  • windows 7 logon hangs during shutdown system asks if you want to force shutdown 2012-05-20

    windows 7 logon hangs during shutdown system asks if you want to force shutdown eventually it shuts down by itself but this problem has just recently started --------------Solutions------------- Hi JOELICKTEIG, 1. Did you make any changes to the comp

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.242 (s). 11 q(s)