Solved

Screen flickering while adding items at runtime..

Posted on 2004-08-30
10
263 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

15 Experts available now in Live!

Get 1:1 Help Now