Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public void ShowForm2()
{
    // Declare my form
    Form2 a = new Form2();
    // Show form
    a.Show();
}

private void mnuSystem_Click(object sender, EventArgs e)
{
    _StatusProgressBar.ProgressBar.Show();

    // Method invoker
    MethodInvoker ink = new MethodInvoker(ShowForm2);
    IAsyncResult a = ink.BeginInvoke(null, null);

    _StatusProgressBar.ProgressBar.Value = 1;
    while (!a.IsCompleted)
    {
        if (_StatusProgressBar.ProgressBar.Value == 100)
        {
            _StatusProgressBar.ProgressBar.Value = 1;
        }
        else
        {
            _StatusProgressBar.ProgressBar.Value += 1;
            System.Threading.Thread.Sleep(10);
        }
    }
    ink.EndInvoke(a);
    _StatusProgressBar.ProgressBar.Hide();
}

I can not show Form2 when I call it like above.
Please help me.
Please tell me why it's wrong?
Posted
Updated 7-Sep-10 21:54pm
v3
Comments
Dalek Dave 8-Sep-10 3:33am    
Minor Edit for Grammar.
Dima Popov 8-Sep-10 4:16am    
Reason for my vote of 1
This is so wrong
thuyphuongid 8-Sep-10 4:48am    
Please tell me why?

This is a pile of God knows what. Your code says that the Invoker should be called from a seperate thread, but the same code also says that you're updating UI controls (ProgressBar), meaning it can only be on the UI thread, which would eliminate the need for the Invoker. So, which is it?
 
Share this answer
 
If you try to invoke something on the UI thread while you are already on the UI thread and you don't continue until that subordinate code completes, the program will block forever since the main code is preventing the UI thread from being freed up for the subordinate code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900