Solved

Screen flickering while adding items at runtime..

Posted on 2004-08-30
10
259 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now