Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problem delay function

Posted on 2007-10-05
26
Medium Priority
?
363 Views
Last Modified: 2008-01-09
Hi,

     I had written a application 3 years ago who use a lot of "critical" timing this app was working just fine.  After a hardware failure I buy a Intel dual core and now I have a real big problem the timing don't work  Here is my code and after it I will explain what is the problem.

private void Delay(long TimeOut)
{
long hrRes=0, hrT1=0, hrT2=0, dif=0;

timeBeginPeriod(1);
QueryPerformanceFrequency(ref hrRes);

QueryPerformanceCounter(ref hrT1);
while(dif < TimeOut)
{
    QueryPerformanceCounter(ref hrT2);
    dif = ((hrT2 - hrT1) * 10000) / hrRes;  // for 1ms scale
    if (dif > 10) Thread.Sleep(1);
    else Thread.Sleep(0);
}
timeEndPeriod(1);
}            

First you must know that it was working on a single CPU machine
if I use this code the Thread.Sleep(x) is now many time longer than before so my timing is too long
if I remove the Thread.Sleep the delay work but the CPU usage is at 100% and the worst thing
is my 3 other running tread is amost stuck.  I wish I am clear, my english is not so good. if you
have any idea you will help me more than you think because this app is very important

Thanks
Sylvain Bissonnette
0
Comment
Question by:SylvainBissonnette
  • 14
  • 10
25 Comments
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026074
Don't know what you control with this procedure, but I see that you are sleeping the thread by 1 ms every loop, and, as you know, processing the loop takes time too.

Thread.Sleep Method works on .Net Framwork 1.0, 1.1, 2.0 and 3.0 so, it's posible that you are now using a more updated version that performs different (more overhead associated at the method). If this is the problem, try uninstalling newer versions that of yours.

As a suggestion: try to relate the TimeOut argument directly to the parameter of Thread.Sleep... maybe:

Thread.Sleep(TimeOut * 1000); // if TimeOut is in seconds this will sleep the thread that number of seconds
0
 

Author Comment

by:SylvainBissonnette
ID: 20026105
The TreadSleep is only there to give some free time to the cpu to make other things,  in the loop if the delay is more than 10ms the sleep is done if not the loop will make the delay with a high precision.  The problem is only there since I upgrade the motherboard with a dual core.  is there another way to give some free time to the cpu without using the Thread.Sleep.  I don't know why but the sleep with only 1ms give now something like 50ms  with the old motherboard the sleep was giving 10ms.  so I need another way to free some time without the sleep.
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026128
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:SylvainBissonnette
ID: 20026186
Here is a part of the code who need delay, remoting will not really help me

private float SendData(byte To,byte[] Buffer,byte Qte)
{
Comm.WaitForWriteCompletion();
Comm.PurgeInput();
Comm.PurgeOutput();
Comm.Parity = Winsoft.ComPort.Parity.Mark;
Comm.WriteByte(To);
Comm.WaitForWriteCompletion();
Delay(base.StartBitDelay);  << --- Need delay to let the UART make is change
Comm.Parity = Winsoft.ComPort.Parity.Space;
CheckSum = 0x00;
Comm.WriteByte(Qte);      // <<<---------Need delay before sending data      
for (i=0;i<Qte;i++)
{
Comm.WriteByte(Buffer[i]);            
CheckSum += Buffer[i];
}
Comm.WriteByte(CheckSum);      
Comm.WaitForWriteCompletion();
Delay(base.CommDelay);

I'm shure that there is a solution to free some CPU time without rewritting 6000 line of code
we can't rewritte an app because the CPU had been change!
      
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026204
"we can't rewritte an app because the CPU had been change!"

I think that's because your code was tunned using specific parameters based on trial and error using a single processor: base.StartBitDelay and base.CommDelay

What is this? a kind of hardware interface's controller?

I see WaitForWriteCompletion() as a mechanism to sync data transfer.

Please post a link to that component documentation to look for a solution.
0
 

Author Comment

by:SylvainBissonnette
ID: 20026323
This application have a GUI interface and 2 background thread, one of those drive the PC UART to communicate with 125 devices via RS485 those devices report there values, states, and many other things.  RS485 communication is like RS232 but on a single pair of wire so you TX your data and you need to wait all the data to be written and again wait 10ms to let the transceiver change there direction to send a reply to the PC.  The delay was not set by try and error with the old CPU when I ask for 10ms I get 10ms and with 70ms I also get 70ms.  The comm thread must work like this everything is base of TX wait and RX.  The WaitForWriteCompletion() is not very usefull due to all the buffer in the system, in fact if the WaitForWriteCompletion() was working I still need my 10ms to let the tranceiver change there direction.
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026351
oh, I see, RS485 has no a sync mechanism. I think you set those periods based on time that those devices take to receive the command and send back the information.

Just an idea: can you monitor the Ready To Send signal? I'm thinking in a poll reading this line until data becomes available.
0
 

Author Comment

by:SylvainBissonnette
ID: 20026370
No I can't RS485 protocol is very basic everything is base on Tx wait and Rx  In 99% of all time the PC is a master and it poll all the slave.  Before my scan time for 120 devices was 1.1sec  now it's 3.5sec.  It's not acceptable the max I can have is 1.5sec.  I don't know how I will say to the customer it's now 3 time slower with a 3 time faster PC.....  Maybe I say the samething again but if I don't make any sleep in my loop my timing is good, but the CPU crank at 100% and the PC became slow
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026374
did you check the framework version?
0
 

Author Comment

by:SylvainBissonnette
ID: 20026431
Here is the info:
Microsoft Visual Studio 2005
Version 8.0.50727.42  (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026491
were you using v2.0 all the time? or upgraded at same time as processor?
0
 

Author Comment

by:SylvainBissonnette
ID: 20026503
I always had 2.0,  I have problem with only one modification by chance I didn't make two, I can't imagine!!!
0
 

Author Comment

by:SylvainBissonnette
ID: 20026509
In the same way, the question could be very simple:
is there a way to give some timeslice to windows if I make a
while(true)
{
}
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026517
have you tried Application.DoEvents();
0
 

Author Comment

by:SylvainBissonnette
ID: 20026546
Yes that's the first I had try, that make a big zero difference,...
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026556
Try looping in a separate thread:

Thread T1 = new Thread(new ThreadStart(MethodName));
T1.Start();
0
 

Author Comment

by:SylvainBissonnette
ID: 20026577
I don't really understand.  you want I start another thread in my delay loop? and what this tread must have into?
0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20026613
Something like this:

DelayMethod(){
   while(true){
   }
}

Then in your main thread change this line:
Delay(base.StartBitDelay);  << --- Need delay to let the UART make is change

for this:
Thread T1 = new Thread(new ThreadStart(DelayMethod));
T1.Priority = ThreadPriority.Lowest;
T1.Start();
T1.Join(MilliSeconds);
0
 

Author Comment

by:SylvainBissonnette
ID: 20027829
Where is the MiliSeconds in T1.Join(MilliSeconds);
0
 

Author Comment

by:SylvainBissonnette
ID: 20027919
Ok, I understand what is the MilliSecond  I try that and give you feedback
0
 

Author Comment

by:SylvainBissonnette
ID: 20027952
I got a error "Method name expected

            Thread T1 = new Thread(new ThreadStart(Delay));
            T1.Priority = ThreadPriority.Lowest;
            T1.Start();
            T1.Join(base.CommDelay);

public void Delay(long TimeOut)
{
      long hrRes=0, hrT1=0, hrT2=0, dif=0;
      timeBeginPeriod(1);
      QueryPerformanceFrequency(ref hrRes);

      QueryPerformanceCounter(ref hrT1);
      while(dif < TimeOut)
      {
      QueryPerformanceCounter(ref hrT2);
      dif = ((hrT2 - hrT1) * 10000) / hrRes;  // for 1ms scale
      }
      timeEndPeriod(1);
                     }
0
 

Accepted Solution

by:
SylvainBissonnette earned 0 total points
ID: 20031023
Finaly I found the solution
- No CPU overload
- High presision delay

[DllImport("winmm.dll")]
internal static extern uint timeBeginPeriod(uint period);

[DllImport("winmm.dll")]
internal static extern uint timeEndPeriod(uint period);
 
[DllImport("kernel32.dll")]
 static extern void Sleep(uint dwMilliseconds);

public void Delay(uint TimeOut)
{
timeBeginPeriod(1);
Sleep(TimeOut);
}
      }

0
 
LVL 5

Expert Comment

by:fmonroy
ID: 20038778
Sorry for the late response, It's good that you finally got a solution, Congrats.
0
 

Author Comment

by:SylvainBissonnette
ID: 20059657
Question solve by my self please refund
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20077939
Closed, 500 points refunded.
modus_operandi
Community Support Moderator
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question