Click here to Skip to main content
15,891,529 members
Home / Discussions / C#
   

C#

 
AnswerRe: Windows Application for Backup using C# Pin
harleydk20-Dec-08 10:40
harleydk20-Dec-08 10:40 
QuestionHow Do I Copy An Embedded Resource to a Directory? Pin
That Asian Guy19-Dec-08 14:19
That Asian Guy19-Dec-08 14:19 
AnswerRe: How Do I Copy An Embedded Resource to a Directory? Pin
N a v a n e e t h19-Dec-08 19:06
N a v a n e e t h19-Dec-08 19:06 
AnswerRe: How Do I Copy An Embedded Resource to a Directory? Pin
Giorgi Dalakishvili19-Dec-08 22:53
mentorGiorgi Dalakishvili19-Dec-08 22:53 
AnswerRe: How Do I Copy An Embedded Resource to a Directory? Pin
#realJSOP20-Dec-08 1:15
mve#realJSOP20-Dec-08 1:15 
QuestionHelp using Threads with an applications SDK [modified] Pin
__rob19-Dec-08 13:47
__rob19-Dec-08 13:47 
GeneralRe: Help using Threads with an applications SDK Pin
Luc Pattyn19-Dec-08 14:25
sitebuilderLuc Pattyn19-Dec-08 14:25 
GeneralRe: Help using Threads with an applications SDK [modified] Pin
__rob19-Dec-08 15:44
__rob19-Dec-08 15:44 
Hi - It could well be the threadpool is running at a lower priority, I will have to look into that, but the CPU does go to 100%. There are No GUI elements accessed, there are a few parameters, but these are all accessed at the start, the whole process of retriving the needed information from the application takes the same time to execute in both threaded and non threaded, so not much of a problem there. I will explain how this works to mabey help give a clearer picture.

EvaluateNormalizedPosition2 is part of the SDK. it is a method available on NurbsSurfaceMesh Objects. The application works by connecting Input and Output ports to the various objects in the scene. Everything is returned from these ports as __System.Object and you cast to the correct Type using the supplied interfaces, such as NurbsSurfaceMesh. There is rather alot of code, and its a bit of a mess, but i'll paste key bits.

The whole operation of this plugin is done through the 'Update Callback' public bool Update(Context in_ctxt){}
the Context Object provides access to these various Input and Output ports that are defined in a Define Callback earlier in the plugin. The relevant 'Input Port' is the one that provides access to the NurbsSurfaceMesh, on which EvaluateNormalizedPosition2 method exists.

My class, 'nurbsInfo' is where I do all the calcuations, and where EvaluateNormalizedPosition2 is used extensively. The constructor of this class takes the NurbsSurfaceMesh as an argument, in which it is assigned to a private variable (_nurbsSurface) of the class . This is how EvaluateNormalizedPosition2 is accessed within my class - _nurbsSurface.EvaluateNormalizedPosition2(u,v)

This is the line within the Update Callback that I create a new instance of my NurbsInfo class and pass the NurbsSurfaceMesh to the constructor.

NurbsInfo nurbsInfo = new NurbsInfo(((NurbsSurfaceMesh)((Primitive)ctxt.GetInputValue(0, 0, 0)).get_Geometry(null)).Surfaces[0]);

In the single threaded version I would then call my GetUVFromPercentage method on the nurbsInfo object, passing it 2 doubles and returning an SIVector3 object.

SIVector3 newVector3 = nurbsInfo.GetUVFromPercentage(50,50)

Within GetUvFromPercentage, the EvaluateNormalizedPosition2 method is called many times on the _nurbsSurface object. The execution time without using the thread pool is quite small for the whole plugin. EvaluateNormalizedPosition2 is called around 30 times. The problem is, this Update Callback is called once for every single instance of the plugin in the application, and these small execution times build up if its calling the plugin (and therefor GetUvFromPercentage) say 50 or more times, one after the other.

My hope was that I could rearrange my plugin, so all 50 or more calculations are part of the same plugin instance in the host application. This just means that all the data such as the desired percetages are all passed at once - I had hoped this would then allow me to thread them, by doing them all in one call of the Update Callback.

Below is the code I use now to call the nurbsInfo.GetUVFromPercentage() method. I have actually modified this methods signature so that it has no return type anymore (as I haven't looked into how to do that when using threading), and accepts an Object as its argument. It seems this is the only way to pass parameters when using QueueUserWorkItem. Having no return should make no differce, it just means I dont pass any data back to the host application.. but all the calculations are still performed.

for (int x = 0; x <= numInstances - 1; x++)
{
autoResetEvent[x] = new AutoResetEvent(false);
}

for (int x = 0; x <= numInstances - 1; x++)
{
surfacePathTProperty = (CustomProperty) customOperator.PortAt(0, 1, x).Target2;
double uLocation = (double) surfacePathTProperty.Parameters[1].get_Value(ctxt.Time);
double vLocation = (double) surfacePathTProperty.Parameters[2].get_Value(ctxt.Time);
Object[] args = new Object[2];
args[0] = uLocation;
args[1] = vLocation;
ThreadPool.QueueUserWorkItem(nurbsInfo.GetUVFromPercentage, args);
}
ThreadPool.WaitAll(autoResetEvent);

So the above code is in the Update Callback, and calling QueueUserWorkItem 50 times with all the different uLocations and vLocations. Instead of having 50 seperate plugins in the Host Application and the application calling the Update Callback 50 times in a serial manner. The only other difference is that in the Threaded version GetUVFromPercentage will .Set the autoResetEvent when its finished.

I am at a loss as to why calls to EvaluateNormalizedPosition2 are small when called in a serial manner, but are huge when the nurbsInfo.GetUVFromPercentage is being executed by one of the ThreadPools threads. I have even when reduced 'numinstances' to1, so there is only 1 Thread, as I thought having 50 all calling SDK methods might be the problem. But the calls to EvaluateNormalizedPosition are still huge. I would have expected it to be very similar execution times to executing 1 Update Callback in the non-threaded version.

Do threads have different prioritys when executing code, or different..something ? I assumed they would execute it in exactly the same manner as the Main Thread does in the non-threaded version, just lots more of them doing it Smile | :) As I understand it, calls to various SDK objects and methods, should be the same whether done from the main thread or a threadpool thread.

I also found that using List<t> or Dictionary<t> .add methods were very slow in the threaded version and also added to the huge overall execution time, I have commented all of these out for now.

Could it just be that the provided .NET sdk does not like being used with threads?. From what I can see (guess) it does look like alot of the .net stuff they have added is wrapping the older c++ api.

I am not knowledgeable enough to know whether this could be the source of the problem.


If you managed to read this far .. any advice ?

Thanks Smile | :) )

modified on Friday, December 19, 2008 9:58 PM

GeneralRe: Help using Threads with an applications SDK Pin
Luc Pattyn19-Dec-08 16:06
sitebuilderLuc Pattyn19-Dec-08 16:06 
QuestionParamter @ Model has no default value? Pin
dec8219-Dec-08 12:34
dec8219-Dec-08 12:34 
AnswerRe: Paramter @ Model has no default value? Pin
PIEBALDconsult19-Dec-08 12:45
mvePIEBALDconsult19-Dec-08 12:45 
QuestionWhat technique to use to keep my application running ... ? Pin
Yanshof19-Dec-08 11:45
Yanshof19-Dec-08 11:45 
AnswerRe: What technique to use to keep my application running ... ? Pin
Joel Ivory Johnson19-Dec-08 12:04
professionalJoel Ivory Johnson19-Dec-08 12:04 
GeneralRe: What technique to use to keep my application running ... ? Pin
Yanshof19-Dec-08 12:55
Yanshof19-Dec-08 12:55 
GeneralRe: What technique to use to keep my application running ... ? Pin
Colin Angus Mackay19-Dec-08 14:40
Colin Angus Mackay19-Dec-08 14:40 
AnswerRe: What technique to use to keep my application running ... ? Pin
Luc Pattyn19-Dec-08 13:12
sitebuilderLuc Pattyn19-Dec-08 13:12 
GeneralRe: What technique to use to keep my application running ... ? Pin
PIEBALDconsult19-Dec-08 13:30
mvePIEBALDconsult19-Dec-08 13:30 
GeneralRe: What technique to use to keep my application running ... ? Pin
Luc Pattyn19-Dec-08 13:48
sitebuilderLuc Pattyn19-Dec-08 13:48 
QuestionA form's minimum height Pin
Andrew Paul19-Dec-08 9:39
Andrew Paul19-Dec-08 9:39 
AnswerRe: A form's minimum height Pin
Dave Kreskowiak19-Dec-08 9:58
mveDave Kreskowiak19-Dec-08 9:58 
GeneralRe: A form's minimum height Pin
Andrew Paul19-Dec-08 10:27
Andrew Paul19-Dec-08 10:27 
GeneralRe: A form's minimum height Pin
Dave Kreskowiak19-Dec-08 15:48
mveDave Kreskowiak19-Dec-08 15:48 
GeneralRe: A form's minimum height Pin
Andrew Paul19-Dec-08 15:53
Andrew Paul19-Dec-08 15:53 
AnswerRe: A form's minimum height Pin
Luc Pattyn19-Dec-08 10:17
sitebuilderLuc Pattyn19-Dec-08 10:17 
GeneralRe: A form's minimum height Pin
Andrew Paul19-Dec-08 10:25
Andrew Paul19-Dec-08 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.