It is currently April 26th, 2024, 12:54 am

[help] C# and multithreading

Share and get help with Plugins and Addons
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [help] C# and multithreading

Post by Aragas »

jsmorley wrote:So as far as I can tell Aragas, it seems to work fine.

I have committed the changes to https://github.com/jsmorley/CheckNet, and will update the CheckNet .rmskin in a few minutes so others can try it out.

Thanks once again for your help with this. I think it is pretty clear how it works, and although this code is pretty specific to my functionality, (with only one thread ever being active at a time for instance) it has gone a long way toward helping my understanding.

I owe you a vodka! :-)
Happy to help!
I'll make some optimizations in my fork. So don't forget to check it out later :oops:
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [help] C# and multithreading

Post by jsmorley »

I have added a "FinishAction" option to the plugin measure, which will do the same thing it does on other "threaded" plugins like WebParser and FileView. It will execute any defined Rainmeter actions when the plugin has "finished" doing all the connectivity tests in the thread.

This is a better way to handle having the measure take some action when the plugin completes, as with threading you can't really depend on OnChangeAction or OnUpdateAction to be a reliable indication that the measure has completed its work.
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [help] C# and multithreading

Post by Aragas »

I think it's better to make a entire function for FinishAction. Will be easier to call it from not-measure class. Check my fork
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [help] C# and multithreading

Post by jsmorley »

To be honest, I think we have a bigger more fundamental problem with the entire plugin and how it is threaded.

Much like WebParser does, this plugin can easily be "hung". In fact, it will hang itself sooner or later if you just leave a skin using it running and checking the network every 5 seconds and the internet every 20 seconds.

I think it is related to the "abort" function we are using to end a running thread when the skin is refreshed (or in the case of this plugin, when the processing done in the thread is completed. I really don't think "abort" should ever be used in the context of Rainmeter, as terminating a thread in an ungraceful way just causes problems.

Rainmeter is fine, and the skin keeps running with no error, but the plugin .dll gets "hung" and just ignores any calls to it from the skin. The only solution is to completely exit Rainmeter, so Windows "cleans up" any orphaned .dll's, and restart Rainmeter. Then it works fine, for a while...

I will have to do some investigating and talking with poiru about a better way to manage these threads. It has been a long-standing problem in WebParser, and I don't want to add another one to the list.
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: [help] C# and multithreading

Post by FlyingHyrax »

jsmorley wrote: I think it is related to the "abort" function we are using to end a running thread when the skin is refreshed (or in the case of this plugin, when the processing done in the thread is completed. I really don't think "abort" should ever be used in the context of Rainmeter, as terminating a thread in an ungraceful way just causes problems.
http://www.albahari.com/threading/part3.aspx#_Abort
Flying Hyrax on DeviantArt
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [help] C# and multithreading

Post by Aragas »

Working on it, big thanks
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [help] C# and multithreading

Post by jsmorley »

Just as an aside, we need to be careful that any .NET functions we use are supported in .NET 2.0.
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: [help] C# and multithreading

Post by FlyingHyrax »

jsmorley wrote: I opened up Process Explorer and watched the threads that Rainmeter was creating and maintaining while the skin was running, unloaded and refreshed, and it seems to work as expected. A new thread is created for a hot heartbeat while it checks the network and / or internet, and then goes away.
It's more than likely that I'm misunderstanding how Process Explorer works, but I can't replicate the behavior you describe. For me, a new thread is spawned every ~5 seconds, with a another thread spawned in addition ~20 seconds. None are deleted immediately; instead after a few minutes they are all cleaned up at once (garbage collection?)
Flying Hyrax on DeviantArt
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [help] C# and multithreading

Post by Aragas »

Ready. Have tested - Threads are closing. Win? Check my fork.
jsmorley - could you check it in Process Explorer?
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [help] C# and multithreading

Post by Aragas »

Lol, broke somehow my git upload. Uploaded it manually.