none
C# dynamically adding huge number of TabPage RRS feed

  • Question

  • I need to add a lot off TabPages in run-time. The problem is that in some moment form moves to "Not Responding" state, but in half of minute continues working. 

    I add TabPage in separate thread using BeginInvoke. 

    ThreadStart^ threadDelegate = gcnew ThreadStart(this,   &MPT1::Log::DisplayLogFile);
    Thread^ newThread = gcnew Thread( threadDelegate );
    newThread->Start();
    
    void Log::DisplayLogFile()
    {
    /*....*/
    tabControlEntryTabsLog->BeginInvoke(gcnew DrawLogTabsDelegate(this, &MPT1::Log::DrawLogTabs));
    }
    
    void Log::DrawLogTabs()
    {
     /*...*/
      // adding tabs
      for each (TabPage^ page in tabsArray)
      {
          tabControlEntryTabsLog->Controls->Add(page);
      }
    }

    How can I fix this issue?

    Thursday, September 3, 2015 2:22 PM

Answers

  • Hi Droll80,

    Per my understanding, there is a short pause when WinForm is trying to add TabPage to TabControl no matter running in main thread or sub thread. An easy work around is to let the sub thread stops for a short while like the following code.

    void Log::DrawLogTabs() { /*...*/ // adding tabs for each(TabPage ^ page in tabsArray) {

    // i am not familiar with c++, you have to translate to c++ by yourself System.Threading.Thread.Sleep(500); tabControlEntryTabsLog->Controls->Add(page); } }

    You can retrieve my code sample from: http://1drv.ms/1hOTBVp



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, September 7, 2015 2:28 AM
    Moderator
  • Try an alternative way too. Create the tabsArray, then call BeginInvoke. In your invoked function, add the first tab of tabsArray to form, then remove it from tabsArray. If the array is not empty, then call again BeginInvoke.

    To simplify, use list instead of array.
    Monday, September 7, 2015 6:19 AM

All replies

  • Try this:

        tabControlEntryTabsLog->SuspendLayout();
        // loop that adds the tab pages…
        tabControlEntryTabsLog->ResumeLayout();

    Since you already observed that your approach does not help in preventing the “Non Responding” state, then maybe do not use the separate thread.

    Also having a large number of tab pages is probably not a frequent practice.

    Thursday, September 3, 2015 7:00 PM
  • Hi, thanks for your answer. I've tried this already. Doesn't help. According to separate thread - this is not the root of the problem. And yes adding a large number of tab pages isn't a good idea, but for now I cannot change design. 
    Friday, September 4, 2015 5:52 PM
  • Hi Droll80,

    Per my understanding, there is a short pause when WinForm is trying to add TabPage to TabControl no matter running in main thread or sub thread. An easy work around is to let the sub thread stops for a short while like the following code.

    void Log::DrawLogTabs() { /*...*/ // adding tabs for each(TabPage ^ page in tabsArray) {

    // i am not familiar with c++, you have to translate to c++ by yourself System.Threading.Thread.Sleep(500); tabControlEntryTabsLog->Controls->Add(page); } }

    You can retrieve my code sample from: http://1drv.ms/1hOTBVp



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, September 7, 2015 2:28 AM
    Moderator
  • Try an alternative way too. Create the tabsArray, then call BeginInvoke. In your invoked function, add the first tab of tabsArray to form, then remove it from tabsArray. If the array is not empty, then call again BeginInvoke.

    To simplify, use list instead of array.
    Monday, September 7, 2015 6:19 AM