how to add delay to a function c#

2847 views c#
4

I want send sensor data to server but it's taking too long, so i decide to use timer to delay between getting data from sensor and then send it to server. But still, it doesn't work. I'm still wonder what's the problem, my code or something else. here's my code

                    foreach (JointType jointType in joints.Keys)
                    {
                        // sometimes the depth(Z) of an inferred joint may show as negative
                        // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                        CameraSpacePoint position = joints[jointType].Position;
                        if (position.Z < 0)
                        {
                            position.Z = InferredZPositionClamp;
                        }

                        DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                        jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
                        }

                    Joint Head = joints[JointType.Head];

                    float x = Head.Position.X;
                    float y = Head.Position.Z;



                    TxtX.Text = x.ToString("#.##");

                    TxtZ.Text = y.ToString("#.##");

                    t.Interval = 1000;
                    t.Start();

                    string sql = "insert into kinect (x, y, time) values (?x, ?y, now())";
                    MySqlConnection koneksi = new MySqlConnection(database);
                    MySqlCommand cmd = new MySqlCommand(sql, koneksi);
                    cmd.Parameters.Add("?x", MySqlDbType.Float).Value = x;
                    cmd.Parameters.Add("?y", MySqlDbType.Float).Value = y;
                    MySqlDataReader reader;
                    koneksi.Open();
                    reader = cmd.ExecuteReader();

answered question

It's taking too long, so you want to delay it ... doesn't make any sense to me, but if you insist, put the part of your code that does that in a task and use Task.Delay.

1 Answer

7

"The Operation takes [Retreival]+[Sending] time. So I want to add a delay so it takes [Retreival]+[Delay]+[Sending] time." That does not sound like the droid you are looking for.

The droid you should be looking for is some form of Multitasking. Long running operations need to be moved into paralell Task - or even alternate Thread - so they do not lock up the GUI Thread. Network (inlcuding DB) and Disk Operations are prime examples of this.

As a beginner in the thematic, I advise to using the BackgroundWorker in a WindowsForms application. At least until you got a handle on stuff like Invoking and race conditions. Then you can look into advanced stuff like Tasks, async/await and the like.

But as you mentioend Kinect in the tags, it is possible you are doing this in some form of Gaming Environment. If so you should propably stop this project and try that out in a easier Environment (again, WinForms + BackgroundWorker is a good starting place).

posted this

Have an answer?

JD

Please login first before posting an answer.