Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

C# 100% CPU Usage using WriteRegenerationMode.DoNotAllowRegeneration

Hello Support 🙂  I've been using DAQs with Labview for some time, but I'm now learning to use them with C# and Measurement Studio and NIDAQ-mx.
 
I want to be able to generate waveforms with arbitrary frequency and modulation, so I'm using

myTask.Stream.WriteRegenerationMode =

WriteRegenerationMode.DoNotAllowRegeneration

I'm using a background thread to keep the buffer filled... this thread writes about 1/4 seconds worth of samples, then sleeps for about 1/4 second.

The problem is that this results in a good waveform, but 100% CPU usage. I've attached a very stripped down version of this program. Please just look at the code, specifically the button1_Click method, and everything will be clear. The code does not appear to be doing very much, so I don't see why this should hose the CPU.

Waveforms with WriteRegenerationMode.AllowRegeneration don't use any CPU.

Thanks for your help!

~Suneet Upadhyay
UCB Physics 111 Advanced Electrical Laboratory

thrawn at berkeley .edu 

Message Edited by SuneetU on 06-15-2007 12:46 PM

Message Edited by SuneetU on 06-15-2007 12:54 PM

0 Kudos
Message 1 of 6
(7,407 Views)
I figured it out!  Don't add to the buffer unless it has room!
 

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {

while (run) {

if (myTask.Stream.OutputBufferSpaceAvailable > 2 * bufferSize) {

writer.BeginWriteMultiSample(

false, data, null, null);

}

Application.DoEvents();

System.Threading.

Thread.Sleep(200);

}

}

0 Kudos
Message 2 of 6
(7,399 Views)
Hi
 
I can not open your attachment file now:)
If you do the Worker at a background threading, you do not need to do a DoEvent() method, it is not always a good way to sychronous the UI, try below way to control UI when you are in a threading.
 
If you want to plot the UI, refer to what I cut from my code:
 
  #region Update 3DControl in correct thread
  delegate void Plot3DDPCDelegate( double[] vectorX, double[] vectorY, double[] vectorZ);
  private void Plot3DDPC( double[] vectorX, double[] vectorY, double[] vectorZ)
  {
   if( axCWGraph3D1.InvokeRequired == false)
   {
    axCWGraph3D1.Plots.Item(2).Plot3DCurve(vectorX, vectorY, vectorZ, Type.Missing);
   }
   else
   {
    Plot3DDPCDelegate plotDelegate = new Plot3DDPCDelegate( Plot3DDPC );
    this.BeginInvoke( plotDelegate, new object[] { vectorX, vectorY, vectorZ});
   }
  }
 
 
Call this kind of method in your background threading, you will have a good performance in CPU usage, if you have many threading at same time, please give a sleep(0) or sleep(10) in every threading, it will force the thread efficiency better.
 
Thanks.
 
 
********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 3 of 6
(7,349 Views)

Hi Sun

I have read your code, I do not think it is a good way you put a very long process in button1_Click event, if a process is up to 20 ms, you are supposed to put it into a threading,

********************************
*The best Chinese farmer*
********************************
0 Kudos
Message 4 of 6
(7,348 Views)

When I use the original program in the ZIP file, I do not get a continuous signal (~50ms sines, ~200ms 0V). I guess this is not intended???

 

When I comment out the line

 

 

System.Threading.Thread.Sleep(250);

 then I get a continuous signal (CPU usage about 17%).

 

 

When I try to use your modification, I get the following error:

You cannot get the specified property, because the task is not a buffered output task.
Property: NationalInstruments.DAQmx.DaqStream.OutputBufferSpaceAvailable
Task Name: _unnamedTask<0>

This site is the only site in the whole internet where I can find anything about "OutputBufferSpaceAvailable".

How do I use this correctly?

 

0 Kudos
Message 5 of 6
(4,851 Views)

Hi Camell,

 

You may want to post as a new topic for this specific question, since this particular forum is rather old.

 

You're right--it doesn't appear this property is widely used.  I understand this property represents the amount of space that is remaining in the PC buffer for AO samples written out.  You can find the .NET or C reference help for various functions in:

 

Start>>All Programs>>National Instruments>>NI-DAQ>>Text-based Code Support

 

In other words, if you wished to programmatically determine what to do based on how much data you still have to be written out to your AO channel, I believe this is the property you would use.  Unfortunately, like you, I wasn't able to immediately find any examples.  You could probably try including this property in one of the pre-existing examples (in the directory I mentioned above) and place an indicator on your front panel to watch how it varies.

 

To learn more on the topic of buffer allocation, you might see this KB about How Buffer Size is Allocated for Acquisitions.  The specific NI-DAQmx Help topic: How is Buffer Size Determined?  mentions the principles used to guide AO buffer allocation.  Like AI, there is a property for manually allocating buffer size for AO called DaqBuffer.OutputBufferSize

 

DAQmx Timing and Sample Rates - Good general resource

 

Hope this helps,

Andrew

National Instruments
0 Kudos
Message 6 of 6
(4,824 Views)