INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: MCAutomation vs IMJAutomationPtr?  (Read 2506 times)

stottle

  • Junior Woodchuck
  • **
  • Posts: 71
MCAutomation vs IMJAutomationPtr?
« on: September 02, 2007, 10:45:46 pm »

I'm new to C# and confused on the differences between the C++ interface to MC12 and the C# interface.  I would appreciate any help I can get.

I've got a working C# start to an application (not a plugin) that populates a list box based on the JRiver interface.  It simply lists all of the songs.  To do this, I use MCAutomation (I'm not doing anything with events at this point).  The code looks something like this:

Code: [Select]
IMJFilesAutomation mySongs = pMC.Search("[Media Type]=audio");
StringBuilder sBuild = new StringBuilder();
for (int i = 0; i < mySongs.GetNumberFiles(); i++)
{
    IMJFileAutomation myFile = mySongs.GetFile(i);
    sBuild.AppendFormat("{0}\n",myFile.Name);
}

This code takes about 3-5 seconds to run, which seems pretty slow to me (I've only got about 2000 tracks in my library).  I'm not sure if I'm not the right track or not, but I implemented something similar in C++ (C++ 2005 express vs. C# 2005 express, if that matters).  The following C++ code takes about 1/2 second (including creating the MC instance):

Code: [Select]
IMJFilesAutomationPtr pFiles = pMJ->Search("[Media Type]=audio");
IMJFileAutomationPtr pFile;
for (int i = 0; i < pFiles->GetNumberFiles(); i++)
{
    pFile = pFiles->GetFile(i);
}

Yes, I know I'm not doing any string processing in the C++, but I can comment out that line in the C# and the time it takes to run doesn't change. 

My thinking (I'm really curious if I'm on the right track or not) is that the C# code is doing a copy (probably shallow) of each file, whereas the C++ is just keeping the pointer.  I tried to change the C# code, using unsafe, to use pointers, but I keep getting errors like the following:

Code: [Select]
Cannot take the address of, get the size of, or declare a pointer to a managed type
Now I know that both my C++ and C# started by pointing to "Media Center 12.tlb", but that C# creates a proxy interface.  Is that why C# thinks that the MCAutomation is managed code?  Is there a way around this?

I can't find a way to use MCAutomation in C++, nor IMJAutomationPtr in C#.  I'm hoping someone can explain the differences and let me know if there is a way to get the performance boost I seem to get from C++.

Thanks!
Brett
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42384
  • Shoes gone again!
Re: MCAutomation vs IMJAutomationPtr?
« Reply #1 on: September 03, 2007, 12:30:17 am »

I wish I could help, but I'm not sure of the answer.

It's possible creating a new IMJFileAutomation object is expensive (you're reusing one in your C++).

The search would be a little faster formatted like "[Media Type]=[Audio]" (notice the brackets).

Also, you could try storing the number of files so you aren't making a GetNumberFiles(...) function call over and over in the for loop.

Logged
Matt Ashland, JRiver Media Center

stottle

  • Junior Woodchuck
  • **
  • Posts: 71
Re: MCAutomation vs IMJAutomationPtr?
« Reply #2 on: September 03, 2007, 07:03:33 pm »

Ok, I agree the performance hit is due to the creation of the IMJFileAutomation each time through the loop.  Is there a way to avoid this in C#? 

I've been searching the web a bunch, but I haven't found an answer yet.  It looks like the COM wrapper C# produces doesn't include any pointer types.  And if I try to load the file directly (using DllImport), it doesn't recognize the pointer types that get returned.  If I try to use IMJFileAutomation* as the return type, I again get the "cannot take the address" error.

I assume the tlb includes a description of the types, but I have no idea how to extract that information into C#.

Any suggestions?

Thanks,
Brett
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42384
  • Shoes gone again!
Re: MCAutomation vs IMJAutomationPtr?
« Reply #3 on: September 04, 2007, 02:47:49 pm »

What if you use unmanaged code, and just handle the reference count yourself?

Any other C# wizards?
Logged
Matt Ashland, JRiver Media Center

cncb

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3106
Re: MCAutomation vs IMJAutomationPtr?
« Reply #4 on: September 04, 2007, 03:07:01 pm »

Strange, I haven't found access from C# to be terribly slow in general.

I doubt it would change much, but you could pull the variable declaration out of the loop so you are just reassigning the variable each time which is essentially what you are doing with the smart pointer in C++:

Code: [Select]
IMJFilesAutomation mySongs = pMC.Search("[Media Type]=audio");
StringBuilder sBuild = new StringBuilder();
IMJFileAutomation myFile = null;
for (int i = 0; i < mySongs.GetNumberFiles(); i++)
{
    myFile = mySongs.GetFile(i);
    sBuild.AppendFormat("{0}\n",myFile.Name);
}

Or you could avoid the variable altogether:

Code: [Select]
IMJFilesAutomation mySongs = pMC.Search("[Media Type]=audio");
StringBuilder sBuild = new StringBuilder();
int nFiles = mySongs.GetNumberFiles();
for (int i = 0; i < nFiles; i++)
{
    sBuild.AppendFormat("{0}\n",mySongs.GetFile(i).Name);
}
Logged
-Craig    MO 4Media remote and player:  Android/TV/Auto | iOS | Windows 10/UWP

cncb

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3106
Re: MCAutomation vs IMJAutomationPtr?
« Reply #5 on: September 04, 2007, 03:13:11 pm »

This code takes about 3-5 seconds to run, which seems pretty slow to me (I've only got about 2000 tracks in my library).  I'm not sure if I'm not the right track or not, but I implemented something similar in C++ (C++ 2005 express vs. C# 2005 express, if that matters).  The following C++ code takes about 1/2 second (including creating the MC instance):

Does the 3-5 seconds include "creating the MC instance" because this does tend to take quite a while from C#?
Logged
-Craig    MO 4Media remote and player:  Android/TV/Auto | iOS | Windows 10/UWP

stottle

  • Junior Woodchuck
  • **
  • Posts: 71
Re: MCAutomation vs IMJAutomationPtr?
« Reply #6 on: September 04, 2007, 03:59:37 pm »

Well, I've got some more interesting results from more controlled testing.

3-5 seconds ->
  1)  about 2.8-2.9 seconds (just for a constructor that calls the loop shown above, not initializing) if MC12 is not running.
  2)  about 5.4 seconds if MC12 is running and playing something (I assume this is simply resource contention in MC12)

All following numbers are for not having MC12 running.

When I took "myFiles.GetNumberFiles()" out of the for loop, it shaved a whopping 200 msec of the time.  Actually, not a bad fix.

When I took created "IMJFileAutomation myFile = null;" outside of the loop, and simply assigned to the GetFile(i) result, that brought the time down to about 1.3 seconds.

Next, I tried unmanaged C++.  There's a lot of convenience in the .Net DateTime/TimeSpan that is hard to match in C++, but for the accuracy here, I used time(), which is accurate to the second.  I looped through all of my files 10 times, so the time wasn't around 1 second.

In the end, when I looped through all audio files and extracted the name and key from each file (10 times through the loop), unmanaged C++ and C# were pretty close.  C# - 12 seconds.  C++ - 9 seconds.  So with my library size (and my CPU), it will take about 1 second either way.

So I will stick with C#.

Thanks for the tips!  Are these more reasonable numbers to you?

Brett
Logged
Pages: [1]   Go Up