hi,
how can i make a delay of some time in c sharp

Recommended Answers

All 14 Replies

Thread.Sleep(X);

where X = milliseconds?

The above will work if you are using threads.
If you are not using a threaded application you could look at the timer class.
What does your application do, and why do you need the timer delay?

hi,
i am drawing a graph. And i dont the graph to be drawn at instance. Instead i want it to be drawn slowly ie. with delay. Also can u tell me how to zoom any particular areas of the graph
Thanx for the replies..

then you could definitely look at the timer class.
You create a timer object and set its interval value. The interval is how many milliseconds occur between 'tick' events. You place code inside an event handler for the 'tick' events and it is processed each time the timer ticks.
You could break down the stages of drawing the graph and then each time the timer ticks you draw the next step.

hi
thanx but i cant place the code inside the timer tick event as all the codes are written in onpaint event.
so can we communicate between these two events in the case of timer

take a look at this article. You could take this approach; move your code out of the OnPaint event and into methods you can call from your tick event.

How you do it depends on how you want to implement the delay. What kind of graph are you drawing, and how did you want to animate it? Did you want each section (axis/labels/bars/lines/etc) to simply appear one after the other, or did you want it to fade in?

hi,
i hv attached a pic of my graph. drawin the graph was not much comple instead the problms like
a) graphics problem ie when the graph is minimized it again starts drawing from the starting when it is maximized.
b) if any other control like button are placed on the form that place remains blank till the graph is completed.
c) i want that wen i move the mouse on the graph the coordinates are displayed...
help plzzzzzzzzz
Also the link u had send me was compleeeetly out my level....i didnt got anything
thanx for the replies

are you drawing this directly onto the form?
You would be better to place it in a picturebox or panel. That way it is seperate from the Form's Paint event and shouldnt interfere with the painting of other controls.
Here is a sample that draws a line graph using a timer. I have put each plotted point into an array and used a bitmap as a graphics buffer to draw each step to. Then each time the tick event fires the next point is plotted.

EDIT - Mistake in code...code removed

heres how it shoulda looked :p

Bitmap bufl;
        Point[] points = new Point[10];
        int point;

        private void btnCreateGraph_Click(object sender, EventArgs e)
        {
            bufl = new Bitmap(pnlGraph.Width, pnlGraph.Height);
            point = 1;
            points[0] = new Point(5, 23);
            points[1] = new Point(25, 67);
            points[2] = new Point(45, 89);
            points[3] = new Point(65, 17);
            points[4] = new Point(85, 23);
            points[5] = new Point(105, 89);
            points[6] = new Point(125, 5);
            points[7] = new Point(145, 57);
            points[8] = new Point(165, 88);
            points[9] = new Point(185, 36);
            using (Graphics g = Graphics.FromImage(bufl))
            {
                g.FillRectangle(Brushes.Black, new Rectangle(0, 0, pnlGraph.Width, pnlGraph.Height));
                pnlGraph.Invalidate();
            }
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (point == 9)
                timer1.Stop();

            using (Graphics g = Graphics.FromImage(bufl))
            {
                Pen line = new Pen(Brushes.Red);
                g.DrawLine(line, points[point - 1], points[point]);
                pnlGraph.Invalidate();
            }
            point++;
        }

        private void pnlGraph_Paint(object sender, PaintEventArgs e)
        {
            if (bufl != null)
            {
                e.Graphics.DrawImage(bufl, 0, 0);
            }
        }

The panels Paint event draws the buffered image to the panel, this way, the graph is persistant after the form has been minimised and restored.

hi,
i think i got it little bit but wats this "pnlgraph" refers to......

sorry i got it. Its the name of the form

nooo...its the name of a panel ON the form. As i said, you are better to draw the graph to a control that way you can invalidate panel to refresh the graph without redrawing the whole form. Likewise, the Form can draw all the controls without having to wait for your graph to finish.

thanx
i moved one more step ahead towards my project.

great, glad i could help. Remember to mark the thread as solved if your question has been answered :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.