0

I have a button event that does the following:

Available = true
while (Available)
{
    var cclient = new RestClient("http://192.168.1.1:1234/api/customers");
    Log.log("http://192.168.1.1:1234/api/grab/customers");
    var crequest = new RestRequest(Method.GET);
    request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
    request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n-----011000010111000001101001--", ParameterType.RequestBody);
    IRestResponse cresponse = client.Execute(crequest);
    Log.log(cresponse.StatusCode.ToString());
    if (cresponse.StatusCode.ToString() == "OK")
    {
        List<Rate> result = JsonConvert.DeserializeObject<List<Rate>>(cresponse.Content);
        var groupedData = from b in result.AsEnumerable()
                          group b by b.First_Name into g
                          select new
                          {
                              First_Name = g.Key,
                              //Count = g.Count(),
                              Qty = g.Sum(x => x.Qty)
                          };
        foreach (var x in groupedData)
        {
            Log.log(x.First_Name + " " + x.Qty.ToString());
        }
    }
    WaitSeconds(); //this waits 2 seconds to give it enough time to write to the log file
}

The while loop is happening...but it doesn't log data every two seconds like I am expecting it to.

I have a button that makes Available false and then it logs data.

I have tried placing this into a background worker and still does not log data.

I'm not sure why though.

Thanks,

  • does it log more often or less often? Is log operation flush the data to the file? – Paweł Łukasik Dec 01 '16 at 22:30
  • Have you tried placing a breakpoint? – ColinM Dec 01 '16 at 22:31
  • It logs only 1 time...I've waited 30 seconds...1 minute...but it only logs when I click the second button which makes the Available variable false thus the while loop doesn't occur. – chancelance Dec 01 '16 at 22:32
  • You can just do 'while(true){}' there's no need to make a variable. Are you sure the compiler isn't detecting an infinite loop and causing the problems? – user3164339 Dec 01 '16 at 22:44
  • @user3164339 but then that would be an infinite loop...I don't want it to be infinite...that is why I made the variable Available...and there is another button that I click to make the variable false. Causing it to jump out of the while loop. It is intended to be a infinite loop until Available is false. – chancelance Dec 01 '16 at 22:47
  • The compiler might not be able to figure out that you have another button to break the loop. I think it's just seeing your code as an infinite loop, not a loop with an 'external break' of sorts. perhaps you should put an if statement in your loop saying: 'if (!Available)break;' to try and point the compiler in the right direction? – user3164339 Dec 01 '16 at 22:49
  • @user3164339 the second button is present and when I click it...the background worker jumps to the backgroundWorker1_RunWorkerCompleted function though, if that was true wouldn't this not happen? since the compiler had no knowledge of the external break? sorry just confused about this code not working how I want it to. - it is working how I want it to...but it isn't logging how I expect it to be logging – chancelance Dec 01 '16 at 22:51
  • Honestly, I'm not an expert in compilers however, I do know that they are designed to watch for patterns and make educated guesses as to what's going on to try and improve compile and run time speeds. To me, this is what's happening: The compiler sees what looks like an infinite loop, guesses it wasn't intentional, and allows the loop to run once to keep from blocking the rest of the code. Does this block of code run on a seperate thread? – user3164339 Dec 01 '16 at 22:56
  • Do yourself a favor and use a logging library like Log4net. No need to re-invent the wheel. – Xavier J Dec 01 '16 at 23:02
  • The wheel use to be wood, now its rubber. They use to always go flat if they had a hole in them, now we have run-flat tires.... even the wheel is still re-invented – user3164339 Dec 01 '16 at 23:16
  • @user3164339 Yeah so I click the first button and it checks to see if the backgroundworker is busy..if it isn't busy it calls the background worker.doworkasync() then in there I run what you see above. So the process is running in the background worker the application is not frozen i can still move around and click the second button without getting the "not responding" message – chancelance Dec 01 '16 at 23:29
  • Sorry i didn't ask you if it was threaded earlier, I probly should have. I think the problem is in your webrequests. If you send the same request too fast, or too much the response is cached and your request isn't actually sent. I think you just need to set the cache control of your request by adding another header for cache-control. http://stackoverflow.com/questions/7242579/how-to-send-cache-control-no-cache-in-http-header – user3164339 Dec 01 '16 at 23:42
  • I have added the following: request.AddHeader("cache-control", "no-cache"); same results though. – chancelance Dec 02 '16 at 00:02
  • http://stackoverflow.com/questions/49547/how-to-control-web-page-caching-across-all-browsers/2068407#2068407 the answer provided by Balus C i think is going to be more helpful with cache-control than I can be (i'm not completely positive that's the only header you'll need). You might want to add an 'else' statement for when cresponse != "OK" so that you can add a breakpoint for debuging when the request wasn't accepted. It's probly a good idea to have it anyway so u can react accordingly when the request fails – user3164339 Dec 02 '16 at 00:13
  • just noticed you have "request.Addheader" and "request.AddParameter", are those suppose to be crequest instead as I don't actually see the variable 'request' used anywhere else and that's where you're setting your header values. – user3164339 Dec 02 '16 at 00:18
  • @user3164339 yeah i forgot the c in front but yep it compiles fine, I'm going to try your if statement idea and see how it goes – chancelance Dec 02 '16 at 15:22

1 Answers1

-1

I was able to solve this by adding the following:

Task taskA = Task.Factory.StartNew(() => RunGet());
taskA.Wait(1000);

Now I get consistent logging.