1

I am working on a .net application in which I have to record data and perform calculations and also at the same time add entries into the database. This complete process should be in each 10 milliseconds of time span Elapsed. This is kind of real time data analysis in c#.

I have done lot of research in order to achieve this, with threads, async/await and thread pools but still not able to get the desired output. Basically I want to record data in every 10 milliseconds and store it in database. Below is the code sample i was working on.

private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
        async void InsertData()
        {
            timer1.Stop();
            int i = await Insert(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture));
            timer1.Start();
        }

        async Task<int> Insert(string value)
        {
            ThreadApp objThread = new ThreadApp();
            objThread.value1 = value.ToString();
            db.ThreadApps.Add(objThread);
            db.SaveChanges();
            int i = 1;
            return i;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            InsertData();
        }

Desired Output:

enter image description here

Please if anyone can suggest me how can i achieve this requirement.

techV
  • 935
  • 3
  • 23
  • 41
  • Is it that the data should be in the db every 10 ms, or is it that you should CAPTURE every 10 ms and there can be a longer delay (100-300 ms) before data is available? – zaitsman Dec 26 '16 at 11:25
  • @zaitsman yes .. i meant data should be in the db in every 10 ms. I agreed there may be some delay before data is available but as per the requirement data should be in db in every 10 ms. – techV Dec 26 '16 at 11:28
  • That's going to be very tough with c#, because just the overhead there could easily eat some 50 ms up. I would suggest looking into unmanaged code, e.g. c/c++ – zaitsman Dec 26 '16 at 11:29
  • not sure if it is doable or not with c# but do you think using threads and async/await one(who have good knowledge of threads and async/await) can give a shot? by d way thanks for the suggestion :) – techV Dec 26 '16 at 11:34
  • Forms.Timers are too slow, you need a timer with better precision. – TaW Dec 26 '16 at 11:40
  • @TaW I have also tried to work with Stopwatch but that was still overhead for me with this kind of requirement. :( Can you suggest anything? – techV Dec 26 '16 at 11:48
  • 1
    Have a look at this: http://stackoverflow.com/questions/24839105/high-resolution-timer-in-c-sharp – Visual Vincent Dec 26 '16 at 11:52
  • The Stopwatch is for _measuring the time_ something takes, not for timing. It does not really have any overhead. – Visual Vincent Dec 26 '16 at 11:53
  • Here's also another alternative: https://www.codeproject.com/articles/98346/microsecond-and-millisecond-net-timer – Visual Vincent Dec 26 '16 at 11:57
  • @VisualVincent thanks for your time buddy!!!! I am looking on the links you had provided. Can you tell me exactly what i can go for? – techV Dec 26 '16 at 12:16
  • Try recording a chunk of data locally in your application every 10ms and implement a queue functionality which helps you to submit the data with that value, the above link by @VisualVincent is great use that and implement a local Queue for your db activities which will help you decrease your overhead – Hatim Nagarwala Dec 26 '16 at 12:20
  • @vivek: Please define "go for". Which one of them you should use, or...? – Visual Vincent Dec 26 '16 at 12:33
  • @VisualVincent by "go for".. i meant which would be more suitable for this kind of desired output according to you. – techV Dec 26 '16 at 12:51
  • _i meant data should be in the db in every 10 ms_ is focusing in the wrong direction. Databases are not really real-time capable, and I don't think you need that. Do you want to record the time of the Db write operation or from the moment the data was collected? – H H Dec 26 '16 at 12:51
  • With some queuing and batching the database part should easily cope with 100 items/second, the real issue is on the data collection side. You have told us nothing about that. – H H Dec 26 '16 at 12:54
  • @HenkHolterman please see the desired output table in above post.. i hope u'll get the answer of your question. I need to have data collected as well as recorded in db in every 10 ms. – techV Dec 26 '16 at 12:55
  • @HenkHolterman data is collected and reflected in the text control in every milliseconds.. the need is to get that text control value in every 10 ms and record it in db. – techV Dec 26 '16 at 13:03
  • @vivek: I've never used either of the two I linked you to. I guess you just have to test them and see which one is the most accurate (you can use a Stopwatch to measure this). – Visual Vincent Dec 26 '16 at 13:50

0 Answers0