tags:

views:

107

answers:

4

I need to lock the entire form if the execution of a perticular process is in progress.

My form contains many controls like buttons, comboboxes. all the controls should be in disabled state if the process is running

Now i'n using the two methods from user32.dll

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(String sClassName, String sAppName);

    [DllImport("user32.dll")]
    public static extern bool EnableWindow(IntPtr hwnd, bool bEnable);

but its not working properly.

Is there any other idea to do this

Thanks in advance

+3  A: 
Form.Enabled = false;

Not working?

leppie
its working , but if i click a button in the (form) disabled state, the action is taking place after the form enabled
Pramodh
@Pramodh: That should not happen. Something else must be wrong. Do you make any other winapi calls?
leppie
@leppie : no... here is a sample code this.Enable=false; Thread.Sleep(5000); this.Enable=true;
Pramodh
@Pramodh: Well that will accomplish nothing.
leppie
+8  A: 

What do you mean with lock?

If you want to prevent the user from making inputs you can set

this.Enabled = false;

on you main form, which will disable all child controls, too.

A solution to prevent the events from fireing is to implement a message filter: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx and intercept the left mouse button.

// Creates a  message filter.
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class TestMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        // Blocks all the messages relating to the left mouse button.
        if (m.Msg >= 513 && m.Msg <= 515)
        {
            Console.WriteLine("Processing the messages : " + m.Msg);
            return true;
        }
        return false;
    }
}


public void SomeMethod()
{

    this.Cursor = Cursors.WaitCursor;
    this.Enabled = false;
    Application.AddMessageFilter(new TestMessageFilter(this));

    try
    {
        Threading.Threat.Sleep(10000);
    }
    finally
    {
        Application.RemoveMessageFilter(new TestMessageFilter(this));
        this.Enabled = true;
        this.Cursor = Cursors.Default;
    }


}
SchlaWiener
If i click a button in the (form) disabled state, the action is taking place after the form enabled
Pramodh
That's true, didn't thought about that but from my point of view that is a expected behavior (if I click a button and the threat is blocked I expect the event to follow asap). But If you really want to, I added a solution to intercept the left click (you probably need to intercept some more messages)
SchlaWiener
+2  A: 

When a control Enabled property is set to false, the interaction with that control and all its children is disabled. you can use that in your scenario, by placing all the controls in a ContainerControl parent, and set its Enabled = false.

In fact, you already have such a ContainerContol - your Form.

Franci Penov
+1  A: 

this.Enable=false; Thread.Sleep(5000); this.Enable=true;

Doing the processing in the GUI thread is bad practice, you should use a BackgroundWorker.

A quick and dirty fix would be to call Application.DoEvents() just before enabling your form.

this.Enable=false; Thread.Sleep(5000); Application.DoEvents(); this.Enable=true;
Catalin DICU