Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using windows form application. I want to draw multiple image on a same form at the same time.
I have two timers with same interval one draws flag and second draws a tree.Now my problem is that the two image draw one after another but i want to draw both images at the time...Can anyone help me..here is my code

C#
Graphics G=this.CreateGraphics();
Point F_P=new Point(10,20);
Point T_P=new point(100,200);

 private void Flag_tick(object sender, EventArgs e)
        {
drawflag();
}
public void drawflag()
{
G.DrawImage(flag,F_P)
}

private void Tree_tick(object sender, EventArgs e)
        {
drawtree();
}
public void drawtree()
{

G.DrawImage(Tree,T_P)
}
Posted
Updated 28-Jul-14 10:02am
v2
Comments
ZurdoDev 28-Jul-14 14:19pm    
Please click on Improve question and post the relevant code. It may just be that you need to call Application.DoEvents() so that UI is handled right away.
Sergey Alexandrovich Kryukov 28-Jul-14 15:15pm    
This is the rare case when the question is answerable even without further clarifications (which would be very useful anyway and putting them could greatly improve the value and the usefulness of the question for OP).

Now, I would like to strongly warn against using Application.DoEvents, especially for the purpose in question. This won't be a solution. Please see my answer.

—SA

1 solution

There is no such thing as "same time". However, it's apparent that you face some real problem because you are doing it wrong. Unfortunately, you did not provide enough information on how you do it, so let's ignore it for now. With my advice, you can easily ensure visually correct presentation.

First of all, you should throw out the idea of drawing anything by the timer code. Instead, you should render graphics only in the overridden method System.Windows.Forms.Control.OnPaint or a handler of the event System.Windows.Forms.Control.Paint and use the instance of System.Drawing.Graphics passed to you as an argument. In both cases, drawing is triggered by the Windows message WM_PAINT. Your timer, as well as any interactive event handler, code in some thread other then your UI thread, should only stimulate re-drawing using System.Windows.Forms.Control.Invalidate. The drawing should be done from some data. Your different timers would update the data (don't forget using lock statement to share data structures between threads) and then invalidate the views. Two different invalidates from different sources, if they are close in time, will merge in just one invalidation (this is a rather complicated mechanism transparent to you; it would take too much to explain it here), so you would see "simultaneous" update of the view.

For further detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

Also, please pay special attention for my past answer How to speed up my vb.net application?[^].

How is that threading thing related to the above? I'll explain. First of all, make sure you don't use the timer type System.Windows.Forms.Timer. Yes, pretty much never. More exactly, you cannot use it if you need any action at least a bit close to periodic, or anything which requires any accuracy in timing at all. This timer is utterly inaccurate, by some well known reasons. Other timers will execute your handler in some thread other than your UI thread. So, you will have the same situation as with explicit use of some other thread. Moreover, by many reasons, using a separate thread explicitly is much more straightforward and more reliable than any timer. So, you are advised to prefer threads anyway.

But you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
NoorKaximi 28-Jul-14 16:16pm    
can you explain with example??
Sergey Alexandrovich Kryukov 28-Jul-14 19:42pm    
It would take too much time. I have a project for CodeProject which answers many related questions at once, but the article is not yet ready. In the meanwhile, please better explain which part is not clear to you? First, make sure you understand Paint/OnPaint, Invalidate and Invoke/BeginInvoke.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900