0

I have problem when switching a panels visibility to false which freezes the application. I have a menu on the left site including several buttons. When clicking a button the respective panel is shown and all others are hidden. This works quite well. But I also used small panels indicating when an error message is received by my app.

When I then click the corresponding button I want the panel to disappear and here it is where the application will get stucked. Unfortunately there is no error message. The code shows the event handler and the called method which is used by all menu buttons.

    private void butVEndpoint_Click(object sender, EventArgs e)
        {
            changePanelVisibility(butVEndpoint, pnlvEndpointContent, pnlErrorvEndpoint);
        }

        private void changePanelVisibility(Button currentButton, Panel currentPanel, Panel currentErrorPanel)
        {
            pnlActiveButton.Height = currentButton.Height;
            pnlActiveButton.Top = currentButton.Top;

            //Chaninging all content panels to visible = false
            pnlAppSetContent.Visible = false;
            pnlEkraContent.Visible = false;
            pnlKYContent.Visible = false;
            pnlSip1Content.Visible = false;
            pnlSip2Content.Visible = false;
            pnlRehmContent.Visible = false;
            pnlViscContent.Visible = false;
            pnlvEndpointContent.Visible = false;

            //changing the relevant content panel to visible = true
            currentPanel.Visible = true;

            //changing the error panel to visible = false -> here the code will get stucked
            currentErrorPanel.Visible = false;

        }

The error panel is set to visible = true during an event which recognizes an incoming message. But the thread is/should already be closed when clicking the button afterwards. Is there something else which I don't see? And why does it work with the other panels and only my "error panels" dont work?

Error panel next to the button where the message arrived

This is the code where the panel is set to visible = true.

private void Endpoint_OnCFXMessageReceived(AmqpChannelAddress source, CFXEnvelope message)
    {
        pnlErrorvEndpoint.Visible = true;
        Application.DoEvents();
    }
Vy Do
  • 46,709
  • 59
  • 215
  • 313
m1ch4
  • 21
  • 4
  • Hve you tried changing the orders of visibility between error panel and contentPanel? I have not worked in Winforms, just a idea – r2018 Jun 04 '21 at 11:06
  • 1
    Have you tried calling SuspendLayout() / ResumeLayout() on the parent/main form control at the start/end of the code section? – jason.kaisersmith Jun 04 '21 at 11:29
  • @r2018: Yes I changed the order already and it always stucks at this specific panel. – m1ch4 Jun 04 '21 at 11:34
  • @jason.kaisersmith: no I didnt. You mean within the changeVisibility method at the start I write suspendLayout() and at the end of this method I write resumeLayout()? – m1ch4 Jun 04 '21 at 11:34
  • @m1ch4 Yes exactly. Worth a try. – jason.kaisersmith Jun 04 '21 at 11:36
  • _an event which recognizes an incoming message. But the thread is/should already be closed when clicking the button afterwards._ Show us this part or double check and debug it. – dr.null Jun 04 '21 at 11:37
  • @jason.kaisersmith: unfortunately it didnt work. – m1ch4 Jun 04 '21 at 11:56
  • @dr.null: I added the code to my post. When debugging the method I have indeed some threads still running. The pointer is on the main thread. How can I know what each thread is for? – m1ch4 Jun 04 '21 at 12:02
  • ah. If `Endpoint_OnCFXMessageReceived` is async callback then you shouldn't access the UI thread's controls in that thread. You should do this `pnlErrorvEndpoint.Visible = true;` in the main thread. Also, I don't think you need to call [`Application.DoEvents();`](https://stackoverflow.com/questions/5181777/use-of-application-doevents). – dr.null Jun 04 '21 at 12:18
  • @dr.null: without Application.DoEvents() the panel does not appear. But maybe it works when I handle it within the main thread. Do you think that the panel or the property is somehow blocked by the async callback? How can I switch to the main thread? Sorry but I have no clue. – m1ch4 Jun 04 '21 at 12:24
  • See [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread). – dr.null Jun 04 '21 at 12:29
  • 1
    perfect @dr.null, I will try it and let you know. – m1ch4 Jun 04 '21 at 12:31
  • 1
    The following code in my async callback made it running: this.Invoke((MethodInvoker)delegate { pnlErrorPanel.Visible = true; }); Then I dont have any troubles anymore when setting the visibility to false afterwards. Thanks dr.null! – m1ch4 Jun 04 '21 at 13:20
  • 2
    It's important that you **never, ever** use `Application.DoEvents();`. If you think that you need it, then your code design is flawed and you need to re-design its behavior. – Jimi Jun 04 '21 at 14:12

0 Answers0