[Closed] progress update from c# assembly
I’ve got a c# assembly which I call from 3dsMax with a script. The c# method I’m calling is processing some data from a file. This process takes a few seconds and I’d like to let the user know everything will be ok with a progressbar. Is there a way to update a progressbar in the maxscript based on the progress this method in the c# assembly is making?
absolutely… these processes are in the same thread. so just pass a pointer to a progress bar to update it in your c# assembly routine.
another method is to organize a progress event in your c# class… it’s probably i would do.
Thanks Denis,
passing my progressbar into the C# method works great.
Could you explain a bit how to do it with the event? Normally, when using an event to update a progressbar a backgroundworker is used. How do I connect the progressbar which sits in my maxscript rollout with another thread in a c# assembly?
Hello Klass,
It depends on what the c# assembly gives you in terms of the process itself. You can sometimes parse the stdout but if it is something like a file processing operation you may not be able to get any extra information except “I sort of working on it”
To update the maxscript rollout, you can start a thread where it calls your c# method, and update via the progresschanged event. This way, you can pass the status of the operation back to the UI thread safely. Using this and the final RunWorkerCompleted handler, you can report the status effectively.
where do you want to listen the event? in mxs thread or in c# assembly?
i woud probably write new backgroundworker_plus class derived from backgroundworker, and add just new Object property – tag. and use this property to pass “ProgressBar” in your case, or its hWnd if it’s not a .net control
Thanks for these suggestions guys. But I’m not clear on how this should work.
My progressbar is in a mxs rollout, it is a dotnet probressbar but it lives in 3dsMax. My method which is time-consuming lives in a separate C# assembly. It uploads a file and can report progress while it’s doing that.
So I’d like my mxs progressbar to listen for progress coming from the C# assembly. I could implement a backgroundworker in the C# assembly and call the reportProgress method but how would it talk to my mxs progressbar?
@denisT isn’t the backgroundworker_plus idea very similar to just passing the progressbar to the timeconsuming method like you suggested before? except that it’s on another thread.
You want to update a mxs rollout, so you don’t need to send the progressbar as an object to the c# assembly. it’s kind of irrelevant whether it is a dotnet progressbar or a maxscript one, as you just need to get the status from the process back into max. So put the c# part in the work thread and send the status of the task to the update thread handler.
another way is to make public event (and class if don’t have one) from your c# assembly.
a backgroundworker will fire this event. and you would be able to listen it anywhere.
another way is to make your backgroundworker public and listen it’s progress and update progress bar yourself via mxs
after thinking a little more i like the idea to make the backgroundworker you use public. so provide a method to return it to mxs.
the worker has to fire ProgressChanged event by calling ReportProgress(Int32). it will raise the event with argument [b]e.ProgressPercentage
mxs [/b]can use it to update a progress bar.
Yes, this is exactly what I’ve done in the end. I’ve added a public backgroundworker to my C# class. In that same class, my method which does the heavy work also calls the backgroundworker.reportprogress method. I’m using the one with two arguments, a percentage and a userstate object. I pass a few more items in that object for richer feedback.
In maxscript, I instanciate my C# class and add the eventhandlers to the backgroundworker: dowork, progresschanged and runworkercompleted. In the dowork handler I put the call to the C# method which does the heavy work. In the progresschanged handler I consume the progress percentage and that userstate object to update my progressbar and write stuff to a label.
It took me a while to figure it out and couldn’t have done it without your help! Thanks a lot.