473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C#-FORM: System.Threading.Timer and threading issues

42 New Member
I want to perform certain tasks at certain intervals (update a GUI). But I also want to be able to update the GUI 'manually' (i.e a button click). How is this best achieved?

I've tried to use a System.Threading.Timer and a TimerCallback, but the problem is that the 'Timer-Tick'-method (that actually performs the GUI update tasks) then executes on a ThreadPool thread, and it seems to exit after a while. I would like the same thread to be alive all the time, and manually be able to access the Timer-Tick-method on that thread as well, when the 'update'-button in the GUI is clicked (timer.Change)..

(Am I making any sense? And is this even possible?)

Part of the tasks performed at certain intervals is ssh-communication, and that's really where the problems arise when the ThreadPool thread exits. Then I get an ssh-exception 'pipe-closed', and I prefer that ssh-session to be up and alive
all the time. I guess I could of course close the ssh-session and re-connect each time the timer ticks, but then the response to the 'update'-click in the GUI would be unacceptedly slow, since my ssh-authorizing takes up to 2 minutes.

Any thoughts or ideas whatsoever? Anyone?
Oct 12 '07 #1
9 4684
MimiMi
42 New Member
I guess the "essence" of this thread could be summed like this:

How to implement a task that is to be automatic as well as invoked by (GUI)-user?
My application (written in C# .NET) is communicating with a remote device through ssh. At certain intervals the application initiates a sequence of commands for the device. I also want to be able to manually perform this "command-sequence" by clicking a button in the gui.

As of now, the command-sequence is performed by using a background thread and a System.Threading.Timer, but the problem is the button click..

Any thoughts or ideas on how this could be implemented?
Nov 1 '07 #2
kenobewan
4,871 Recognized Expert Specialist
So you want to continue the timer after clicking the button?
Nov 1 '07 #3
MimiMi
42 New Member
So you want to continue the timer after clicking the button?
Yes, that is what I want!
Nov 1 '07 #4
Plater
7,872 Recognized Expert Expert
Why not just use a windows.forms.timer? you can have an event fire at set intervals and still use the functions it uses?
Nov 1 '07 #5
MimiMi
42 New Member
Why not just use a windows.forms.timer? you can have an event fire at set intervals and still use the functions it uses?
Hmmm..I think (don't know though) that it might be a problem since the tasks that are to be performed when the timer ticks are relatively time consuming non-UI processing, and may also raise events for the GUI to update..
In other words, seems bad to me to perform non-UI processing on the thread that controls the user interface..? (I might just be confused here.. but isn't all windows.forms.timer - related code run entirely on the UI-thread?)
Nov 1 '07 #6
Plater
7,872 Recognized Expert Expert
Yes you are correct.
I thought maybe all you needed done on the tick interval was checking to see if data had come in on the ssh terminal.

You have a tricky situtation, since you're not supposed to acces UI components outside of the UI thread (it will give you an exception about threadsafe with a bunch of options to fix it)

Could you not create some functions that perform your operations, then have a timer (threading.timer? timers.timer?) that calls those functions on the interval, then you could have your button presses call those functions as well?
Nov 1 '07 #7
MimiMi
42 New Member
Could you not create some functions that perform your operations, then have a timer (threading.timer? timers.timer?) that calls those functions on the interval, then you could have your button presses call those functions as well?
Seems like the most simple and straightforward way to do it, yes.

I think the main problem I've been having is keeping the ssh-session alive all the time, AND accessing that ssh-session from another thread than the one it runs on when performing the operations.. and that's probably what's been making me try to "do everything on the same thread".. (and hence making it all quite difficult)

So, the solution is probably to have a background thread alive all the time (the ssh-session) and have another thread (timer) run the functions that perform my operations at certain intervals, and have yet another thread run the functions when the gui button is pressed.

I just need to figure out how to access the ssh-session from other threads than the one it runs on.

Even though I'm far from having it all solved, I think your replies have helped me (think in different ways), so for that I'm really grateful! Thanks!
Nov 1 '07 #8
Plater
7,872 Recognized Expert Expert
Make the SSh object public?
Nov 1 '07 #9
balabaster
797 Recognized Expert Contributor
This seems like a task for a delegate...I would create a method that does the work and embed that in my form...such that (bear in mind that the code I've written below is conceptual - I've not tested it so you really need to understand what it's supposed to do rather than just dropping it into your code):
Expand|Select|Wrap|Line Numbers
  1. 'Declaring your SSH object here means you can open it on your main 
  2. 'thread and keep updating it from your timer as well as from your main form via 
  3. 'the RunTheTask method.
  4. Private MySSHObject As Object 'Whatever it is
  5.  
  6. 'This delegate basically gives us the ability to pass a method address between
  7. 'routines rather than a value.  We can use this method to keep invoking the
  8. 'delegate until InvokeRequired is no longer true, at which point our code will run.
  9. Private Delegate Sub RunTheTaskDelegate(<Add any parameters you require in here>)
  10.  
  11. 'This is the RunTheTask method that really does all the work.  Any requirement 
  12. 'to send data via the SSH object should go through the RunTheTask.  Any 
  13. 'parameters you specify in the parenthesis her must also be applied to the 
  14. 'delegate sub above and vice versa.  Basically interpreted, the method 
  15. 'signatures of your main method and your delegate need to match.
  16. Private Sub RunTheTask(<Add any parameters you require in here>)
  17.   If Me.InvokeRequired Then
  18.     Me.Invoke(New RunTheTaskDelegate(AddressOf RunTheTask), <Add all your parameters from your header here, comma separate them>)
  19.   Else
  20.     'This is where the rest of your code goes
  21.     'Check SSH is connected, if not, connect it
  22.     'Run the code...
  23.   End If
  24. End Sub
  25.  
  26. 'Timer elapsed event
  27. Private Sub Timer_Elapsed(Blah blah blah...) Handles MyTimer.Elapsed
  28.   RunTheTask
  29. End Sub
  30.  
  31. Private Sub Button_Clicked(Blah blah blah...) Handles MyButton.Clicked
  32.   RunTheTask
  33. End Sub
This is the same way you run any task within your form from multiple threads...you could equally call the RunTheTask method from a self created thread.

Obviously this would need to be translated to C# ;) The concepts are all identical though.

Expand|Select|Wrap|Line Numbers
  1. public delegate void MyDelegate(string MyParam1)
  2.  
  3. public void MySub(string MyParam1)
  4. {
  5.   if (this.InvokeRequired) {
  6.     this.invoke(new MyDelegate(MySub), MyParam1);
  7.   else {
  8.     //Do stuff
  9.   }
Hope that provides some insight...
Nov 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan...
4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
2
by: steve | last post by:
Since System.Threading.Timer uses the threadpool to do its stuff what happens when (a) You try to create a timer and the thread pool is *exhausted* (b) The timer is due to fire AND all threads...
6
by: whtinkm | last post by:
Hi, All Recently, my project need some code like following: using System; using System.Threading; namespace MyTimerTest { class Class1 {
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
4
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.