Timer, devideFound and Background Thread!
Hi and thank you for coming to try to help me. I have my code done and working already, but there is on thing bothering me about it, related to the Title of this post.
My problem is, that I want to call "DeviceFound" (Mono.nat lib) every 120s due to the expiration of the port forward being 120s. I tried making a timer which will repeat the deviceFound every two minutes, but it didn't work, the same for creating a new thread, as all the treads will get blocked due to the last one being always active and the program not wanting to do anything till it is exited, but that would mean a restart of the server, which is the thing I want to avoid, I added the related Code down below, excluding the timer and thread I have tried making. I really appreciate any kind of help!
16 Replies
why are you calling
InitializeComponent
so many times
also you are settings the expiration of the upnp port forward to 120 s right?
is there a reason why it's not greater?
and what does it mean that when you used the timer to call (i guess) CreatePortMap it "didn't work"It doesn't want to do it any loget than that, any number higher that 120s will result in it being compressed down to 120s again.
I think that this is a security mesure or so, but I can't relate to why they don't let you just set it any higher.
The UI is in Wpf therefore to ensure an UI update, cus otherwise it didn't update, cus of an unknown reason for me.
the thing that didn't work was the repeated use of the deviceFound method. It just didn't initialize.
It did initialize the first time but any other attempt did not work, as I output the result to the console for debugging and it has always just shown the use of the protocol once.
did you try calling it 20, 50, 100 times to be sure it works and you don't get blocked by the upnp device?
No, but I'm gonna try,...tho I am able to send it if I stop the server manually and start the portforward process again, so I'am not sure,...but well I'm going to try calling it again, just like every secound or so.(manually by creatin a new project with just the upnp thing)
explain this
there I tried to catch the previous console output, tho then I realized that it just is for the user input, and not for the accual "portnumname" which is the protocol info, which I wanted to output. But cus it is inside a foreach it does not update in the UI and the portnumname will always end up being empty. So basicall I forgot to take that out.
and it's a good candidate for why stuff is blocking
hmm I see.
I've a question to ask: won't the last thread(starting run.bat [not initializing it]), stop all parallel threads of the app, as the server stays open/running (in a separate window) and my assumption is: cus of the process still running. ...?
...as I have set a new timer, waited for a good 10min after and the port-forwarding was executed just once.
umm..Here is the log after 20 min right after the "ServerProgram()":
All of the threads, which go one after another are the timer, tho does this mean it got really executed? I don't see the "Protocol: ", or is this kind of behavior normal?
Here is the code, which the log originates from:
the logged threads are from the framework, they're not necessarily created by you
also
TimerElapsed
is not good because you're reassigning events to NatUtility without ever removing them
i don't really know how that class works so i don't know if it has some methods you have to call to configure it or for resetting it
then why are you creating the timer in the elapsed, timer is periodic
maybe you mean the last process?
anyway to me code is a little too messy to debug it
if i was you i'd start from scratch and add piece by piece solving each problem
don't call InitializeComponent outside contructor
pay attention to the behavior that you want to have, for example runproc.WaitForExit();
could block your application tooallright, thanks for helping me out so faar anyways, I'm gonna try doing a structur-diagram first and building arround it then I guess.
yes yes
im not going to say a diagram isn't useful, but probably it's more a matter of understanding the stuff you are using and trying debug the code with the ide to look if it behaves as you're expecting
I see,... thx.
Update: So if you care...it turned out that Device Lost is automatically called whenever the Port-Mapping expires, this way I've just put in the same line of code to create a portmat. The result is quite obvious considering this loop works (it does indeed work).
was this documented?