Solved

Screen flickering while adding items at runtime..

Posted on 2004-08-30
10
267 Views
Last Modified: 2010-04-15
Hi,

In my application, I have a Panel control when I add controls at runtime, dock them on panel top....and populate them...the screen flickering occur....It looks like item stays at the top of the pannel for 3-4 seconds until it gets populated...How I can solve this issue...

Thanks for the help, nauman.
0
Comment
Question by:nauman_ahmed
10 Comments
 
LVL 1

Expert Comment

by:CrazyIvan007
ID: 11933933
You can use

BeginUpdate()

at the Panel before adding the Controls, and then

EndUpdate()

But i think the problem is, that you run it all in the UI Thread, which will stop the UI for a while.

So you can let the Adding of the Controls run in a seperate Thread. For this you have to generate  a Delegate to Add the Controls to the UI (otherwise it's not Safe)

public delegate DelegateAddControl(System.Windows.Form.Panel pnl, Control ctrl);

public class yourapp
{
   private DelegateAddControl m_DelegateAddControl;
   Thread[] t_AddControl = new Thread[1];

   // after InitializeComponent();
   m_DelegateAddControl = new DelegateAddControl(this.AddControl);


   private void AddControl(System.Windows.Form.Panel pnl, Control ctrl)
   {
      pnl.Controls.Add(ctrl);
   }

   private void AddControl()
  {
      t_AddControl[0] = new Thread(new ThreadStart(this.AddControlThread);
      t_AddControl[0].Start();
   }

   private void AddControlThread();
   {
      // Create your Control
      this.Invoke(this.m_DelegateAddControl, new object[] { this.paneltoadd, _controltoadd });

       // more you want to add
    }

}
0
 
LVL 25

Author Comment

by:nauman_ahmed
ID: 11935864
Thanks for the reply...Actually I am planning to program like this:

1. In Main form, I add controls on runtime depending on the user selection. This consist mostly on ListView.
2. I populate the list view using a class called UIDataManagement where I pass the ListView as a parameter.

Now where I should use a new thread? In UIDataManagement or in the MainForm? Also I tried to run an example based on your solution but it didn work...:(

-Nauman.
0
 
LVL 1

Expert Comment

by:CrazyIvan007
ID: 11936253
The Thread should run from the MainForm.

In the Thread you can add the ListViews and Populate them with the data. Otherwise you have to create new Threads for UIDataManagment.

Because you will change Controls in the UI Thread you have to use Delegate, otherwise it will be possible that your App throws Exceptions.

You can try it without Delegate, but it isn't Thread Safe. For more Information look at

http://www.google.de/search?hl=de&ie=UTF-8&q=c%23+delegate&meta=

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

by:somnatic
ID: 11939069
Don't even try it without .. Strange behaviours can occur (believe me .. i had to find that out by myself ... )

Even if you won't get exceptions, some things won't work properly ..
0
 
LVL 11

Accepted Solution

by:
Agarici earned 250 total points
ID: 11942135
if you will use a thread to load the contents of the list, use contol.invoke to call the add function of the listview items collections.
otherwise you will get exceptions... even if it runs ok once, the second time may throw an exception...

btw, unless you have a realy huge list to load, creating another thread to load the items into the listview is not a good idea

hth.
A.
0
 
LVL 25

Author Comment

by:nauman_ahmed
ID: 11943483
Thanks for the reply experts. Actually this application is an outlook 2003 style application with 5 different navigation panes. Each navigation pane has different list view that I add depending on the user selection. As far as adding controls on runtime concerns, I dont have any problem with it. However, when I add the control at runtime and then populate the list view data, it displays a grey box until the control is filled and then docked top in the pannel. What I am thinking is to add controls on runtime using the main thread and populate the data with another thread. Will it be ok?

Thanks for the help, Nauman.
0
 
LVL 1

Assisted Solution

by:CrazyIvan007
CrazyIvan007 earned 250 total points
ID: 11943583
This will work too! The problem is the populating of the ListView which needs some time, if you let them run in a Thread the UI can build up the other Controls without waiting until the ListView is populatet.



0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Exceptions 3 44
designing in object programming 12 76
Simple Injector with Web Service 4 40
updating order of the rows, N+1 query issue, C#, asp.net, MVC, SQL Server 3 26
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question