It is currently April 26th, 2024, 5:36 am

[help] C# and multithreading

Share and get help with Plugins and Addons
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

FlyingHyrax wrote: My fault for naming the thread "C# and multithreading". Tad bit too broad. ;)
:-) Sorry....
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [plzhelp] C# and multithreading

Post by Aragas »

Ready. Code is dirty, but it works. I will make it good tomorrow.
UPD:
Don't touched UpdateCounter, for what, you say, is that thing?
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

I'll have to look more carefully at it, but I think there might be some confusion about what it does.

It has two possible values for the ConnectionType option on the measure.

If the value is "network", then it simply uses NetworkInterface.GetIsNetworkAvailable() to see if the computer is connected to any network. Returns 1 if yes, -1 if no.

If the value is "internet", then it still checks NetworkInterface.GetIsNetworkAvailable() to be sure that there is a network present (as checking for the internet is pointless if there isn't) and then does the Dns.GetHostAddresses("www.msftncsi.com") to see if the internet is available. Returns 1 if yes, -1 if either there is no network at all, or if the DNS function fails.

Unless I'm misunderstanding something, I'm not sure I see those separate functions in the way you have done the code.
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

Aragas wrote:UPD:
Don't touched UpdateCounter, for what, you say, is that thing?
UpdateCounter is tied to the UpdateRate option on the measure. It is similar in how it works to the UpdateRate option on the WebParser measure. The purpose at the end of the day is to enforce a "default" update rate for the plugin. Since UpdateDivider is something that is controlled entirely by the standard measure code, (and not the plugin) UpdateRate is used so I can set a default of 20 updates between executions of the DNS checking. It can be overriden by the user of course, but we would never want to be checking the DNS every second if the user leaves off UpdateDivider on the measure, which would be a default of UpdateDivider=1.

So what happens is UpdateRate is read from the measure when the plugin is loaded, and then on each update it checks to see if UpdateCounter is zero (run now) or non-zero. (don't execute the actions, but instead increment UpdateCounter until it is equal to UpdateRate, then set it to zero.)
User avatar
Aragas
Posts: 64
Joined: December 24th, 2012, 6:56 pm

Re: [plzhelp] C# and multithreading

Post by Aragas »

jsmorley wrote:I'll have to look more carefully at it, but I think there might be some confusion about what it does.

It has two possible values for the ConnectionType option on the measure.

If the value is "network", then it simply uses NetworkInterface.GetIsNetworkAvailable() to see if the computer is connected to any network. Returns 1 if yes, -1 if no.

If the value is "internet", then it still checks NetworkInterface.GetIsNetworkAvailable() to be sure that there is a network present (as checking for the internet is pointless if there isn't) and then does the Dns.GetHostAddresses("www.msftncsi.com") to see if the internet is available. Returns 1 if yes, -1 if either there is no network at all, or if the DNS function fails.

Unless I'm misunderstanding something, I'm not sure I see those separate functions in the way you have done the code.
Okay, yet i understand how its work. Easy to fix, i'll make it tomorrow. 8-)
UPD:
Hm. About UpdateCounter. Yea, it should be in thread. Will make it too.
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

Aragas wrote: Okay, yet i understand how its work. Easy to fix, i'll make it tomorrow. 8-)
Cool. It is important to understand that in the context of this plugin, there is a difference between "network", and "internet"

Network is just that your computer's network adapter is enabled, and that you are connected to some some LAN. (local area network) Most likely it will mean you are connected to a router or a modem provided by your ISP.

NetworkInterface.GetIsNetworkAvailable() has nothing at all to do with the "internet". It is defined as "A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface."

Internet means that you can actually go outside of your LAN to the WAN (wide area network) and access a remote site. We test that by connecting to the DNS server defined in Windows, and resolving the IP for that "www.msftncsi.com" host address.

I have designed it to support the same thing Windows 7+ does on the network icon on the taskbar. If you have no "network" at all, the icon gets a red "X" on it. If the network is ok, but you can't access the "internet", then you get a yellow "!" on the icon.

http://blog.superuser.com/2011/05/16/windows-7-network-awareness/

I don't do the step where they connect via HTTP to "www.msftncsi.com" and get the text contents of the site, as that can just as easily be done with WebParser if that additional test is desired. So in effect, if you use CheckNet and then WebParser, you can get the full functionality of that "Network Connectivity Status Indicator" (NCSI) Windows provides in your skins, including the "connected to the internet but blocked by a login page at some coffee shop / hotel / etc." bit.

In truth, the "network" checking would never need to be threaded. It is a simple call to to a entirely local Windows API and is always instantaneous. However it is possible (if unlikely) that the connection to the DNS server for testing "internet" could be slow, so that really needs to be threaded to be safe. It is best to just do both in the same thread however, as I have made the "internet" testing dependent on the results of the "network" testing. There is absolutely no point in trying to get to the internet if you have no network.
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

Aragas wrote: Hm. About UpdateCounter. Yea, it should be in thread. Will make it too.
I'm not entirely convinced this is true. If UpdateCounter is not equal to zero, then nothing should be executed at all, and perhaps there is no need for a thread to spawn. The entire point of UpdateRate / UpdateCounter is to keep the actions from executing on every update of the skin (the measure in the skin more specifically) and I'm skeptical that we want to spawn a new thread every one second (Update=1000 x UpdateDivider=1) when 19 times out of 20 it will just find that UpdateCounter is not equal to zero and immediately end.

I'm not sure what the "right" way to approach this is. While the DNS check normally takes less than one second to finish, I am hesitant to have the plugin opening new threads every one second, even if they don't do much. UpdateRate is designed to have the entire thing be "skipped" after the first execution, unless UpdateCounter has reached the value of UpdateRate.

Poiru or Brian may have a better feel for this due to their experiences with threads in C++ in the context of Rainmeter.

Feels to me though, that UpdateRate in this case means "every 20 measure updates", not "every 20 times the thread completes". They may not in fact be equal if the DNS check really is "slow".

The flow to me sorta feels like:

Code: Select all

Update()
If UpdateCounter is zero
 Start thread 
 Execute actions
 Thread ends and plugin value is set when actions are finished
Else
 Skip thread and actions
Endif

Increment UpdateCounter
If UpdateCounter is equal to UpdateRate
 Set UpdateCounter to zero
Endif

Return existing or new plugin value from actions.
UpdateRate to me, both in WebParser and this, is to give the user a way to "allow time" for the actions to complete, with some default that makes sense if they don't specify. It isn't meant as a way to "enforce" a rate based in a variable way on how long any given action in the thread takes to complete.
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: [plzhelp] C# and multithreading

Post by FlyingHyrax »

jsmorley wrote: Poiru or Brian may have a better feel for this due to their experiences with threads in C++ in the context of Rainmeter.
+1

For my part, I'm reaching the point of despair...
hyrax wrote:It hangs Rainmeter? Fantastic! It crashed it before. Though when it crashed, I didn't have to bother exiting Rainmeter before copying a new plugin build into the settings directory...
I might need to take a day off. :P
Flying Hyrax on DeviantArt
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [plzhelp] C# and multithreading

Post by jsmorley »

FlyingHyrax wrote: +1

For my part, I'm reaching the point of despair... I might need to take a day off. :P
Oh, I thought you had your's working ok. Now I really am sorry for hijacking the thread.
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Re: [plzhelp] C# and multithreading

Post by FlyingHyrax »

jsmorley wrote: <snip> Now I really am sorry for hijacking the thread.
It's fine. :) I'm putting myself through this for the education, so as long as one of us ends up with a threaded plugin and I can understand how it works, I'm happy as a... something happy. ;) Besides, the thread title!

Update:
Well, I feel rather silly: I totally missed this.
Brian already made a plugin that does exactly everything I ever could have possibly wanted mine to do. Fancy that. :)
Mr. Morley, I cede the thread. :bow:
Flying Hyrax on DeviantArt