473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Timer blocking problems...

Hi to all,

i have a form with 2 System.Windows.Forms.Timer objects.
One fire every 5 seconds and the other every 10 seconds,
the both take actions in two hashtables declared in same form.

When timers fire, main form is somewhat blocking until timers finish
their job, (socket operations). (Imagine to move the form by it's caption
bar and it somewhat freeze when timers fire...)

How i can design this so this blocking do not happen ?

Imagine that i am building an application like Sql Server's
Enterprise Manager, it continuously talks(in the background)
via sockets so it can display it's registered servers' status but it allows
you
to work smoothly with it in the foreground.

I tried to spawn a thread to accomplish that
(ThreadPool.QueueUserWorkItem(new WaitCallback(CheckPendingServants));)

but the problem remains.

I also thought that because these 2 timer routines take actions on the
forms' hashtables,
the code running in these 2 threads will have problems to communicate with
form's main thread
and needs some delegate mechanism to do so.
I also build this too but problem remains...

My last thought is to create a second form with these 2 timers and hide it,
this form will do all work and just take actions on the -now- public
hashtables
on the main form.

Does any other idea comes to mind ?

Thanks a lot
anthonyb

Nov 15 '05 #1
4 3995
Anthony,

Using another thread is definitely the way to go. However, you don't
want to use System.Windows.Forms.Timer, as that will give you the
notifications on the UI thread, which is causing a problem.

The Timer class in the System.Timer namespace will help you. You can
create it on your UI thread, but the event notifications will come in on
another thread. You can then handle all of your processing in this event
handler. However, when updating the UI, you will have to use calls to
delegates through an Invoke method on a control in the UI so that the calls
to update the UI are on the UI thread. Otherwise, do all of the processing
on the other thread.

Also, I believe that the event is fired on a thread from the thread
pool. If your processing is long running, then you might want to throw the
processing in another thread (so that you don't tie up the thread pool).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anthony Boudouvas" <an******@mediatrel.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi to all,

i have a form with 2 System.Windows.Forms.Timer objects.
One fire every 5 seconds and the other every 10 seconds,
the both take actions in two hashtables declared in same form.

When timers fire, main form is somewhat blocking until timers finish
their job, (socket operations). (Imagine to move the form by it's caption
bar and it somewhat freeze when timers fire...)

How i can design this so this blocking do not happen ?

Imagine that i am building an application like Sql Server's
Enterprise Manager, it continuously talks(in the background)
via sockets so it can display it's registered servers' status but it allows you
to work smoothly with it in the foreground.

I tried to spawn a thread to accomplish that
(ThreadPool.QueueUserWorkItem(new WaitCallback(CheckPendingServants));)

but the problem remains.

I also thought that because these 2 timer routines take actions on the
forms' hashtables,
the code running in these 2 threads will have problems to communicate with
form's main thread
and needs some delegate mechanism to do so.
I also build this too but problem remains...

My last thought is to create a second form with these 2 timers and hide it, this form will do all work and just take actions on the -now- public
hashtables
on the main form.

Does any other idea comes to mind ?

Thanks a lot
anthonyb

Nov 15 '05 #2
The Timer class in the System.Timer namespace will help you. You can
create it on your UI thread, but the event notifications will come in on
another thread. You can then handle all of your processing in this event
handler. However, when updating the UI, you will have to use calls to
delegates through an Invoke method on a control in the UI so that the calls to update the UI are on the UI thread. Otherwise, do all of the processing on the other thread.
I did try that before my post here and i was facing the same problem.
I drop a Timer control in my form, not one of the Winforms controls but the
other in the component palette which is a System.Timer control
as you say.
Nothing changed. I do not know if you mean to create a System.Timer
programmatically and create and connect an event handler in it,
but i think that is the same as the System.Timer control on the form,
isn't it ?

As with the solution of the hidden secondary form, what is your opinion
in that?
Thanks a lot for the help!

anthonyb


Also, I believe that the event is fired on a thread from the thread
pool. If your processing is long running, then you might want to throw the processing in another thread (so that you don't tie up the thread pool).
No it isn't a long running process, it is just a try/catch, creating a
TcpClient
on a apecified computer:port and it last below of 2 seconds.



Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anthony Boudouvas" <an******@mediatrel.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi to all,

i have a form with 2 System.Windows.Forms.Timer objects.
One fire every 5 seconds and the other every 10 seconds,
the both take actions in two hashtables declared in same form.

When timers fire, main form is somewhat blocking until timers finish
their job, (socket operations). (Imagine to move the form by it's caption bar and it somewhat freeze when timers fire...)

How i can design this so this blocking do not happen ?

Imagine that i am building an application like Sql Server's
Enterprise Manager, it continuously talks(in the background)
via sockets so it can display it's registered servers' status but it

allows
you
to work smoothly with it in the foreground.

I tried to spawn a thread to accomplish that
(ThreadPool.QueueUserWorkItem(new WaitCallback(CheckPendingServants));)

but the problem remains.

I also thought that because these 2 timer routines take actions on the
forms' hashtables,
the code running in these 2 threads will have problems to communicate with form's main thread
and needs some delegate mechanism to do so.
I also build this too but problem remains...

My last thought is to create a second form with these 2 timers and hide

it,
this form will do all work and just take actions on the -now- public
hashtables
on the main form.

Does any other idea comes to mind ?

Thanks a lot
anthonyb


Nov 15 '05 #3
Anthony,

You could always spawn your own thread and then call Sleep on it for the
specified amount of time. Does that work for you?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"anthonyb" <an******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
The Timer class in the System.Timer namespace will help you. You can
create it on your UI thread, but the event notifications will come in on
another thread. You can then handle all of your processing in this event handler. However, when updating the UI, you will have to use calls to
delegates through an Invoke method on a control in the UI so that the calls
to update the UI are on the UI thread. Otherwise, do all of the

processing
on the other thread.


I did try that before my post here and i was facing the same problem.
I drop a Timer control in my form, not one of the Winforms controls but

the other in the component palette which is a System.Timer control
as you say.
Nothing changed. I do not know if you mean to create a System.Timer
programmatically and create and connect an event handler in it,
but i think that is the same as the System.Timer control on the form,
isn't it ?

As with the solution of the hidden secondary form, what is your opinion
in that?
Thanks a lot for the help!

anthonyb


Also, I believe that the event is fired on a thread from the thread
pool. If your processing is long running, then you might want to throw

the
processing in another thread (so that you don't tie up the thread pool).


No it isn't a long running process, it is just a try/catch, creating a
TcpClient
on a apecified computer:port and it last below of 2 seconds.



Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Anthony Boudouvas" <an******@mediatrel.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
Hi to all,

i have a form with 2 System.Windows.Forms.Timer objects.
One fire every 5 seconds and the other every 10 seconds,
the both take actions in two hashtables declared in same form.

When timers fire, main form is somewhat blocking until timers finish
their job, (socket operations). (Imagine to move the form by it's caption bar and it somewhat freeze when timers fire...)

How i can design this so this blocking do not happen ?

Imagine that i am building an application like Sql Server's
Enterprise Manager, it continuously talks(in the background)
via sockets so it can display it's registered servers' status but it

allows
you
to work smoothly with it in the foreground.

I tried to spawn a thread to accomplish that
(ThreadPool.QueueUserWorkItem(new WaitCallback(CheckPendingServants));)
but the problem remains.

I also thought that because these 2 timer routines take actions on the
forms' hashtables,
the code running in these 2 threads will have problems to communicate with form's main thread
and needs some delegate mechanism to do so.
I also build this too but problem remains...

My last thought is to create a second form with these 2 timers and

hide it,
this form will do all work and just take actions on the -now- public
hashtables
on the main form.

Does any other idea comes to mind ?

Thanks a lot
anthonyb



Nov 15 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Of**************@TK2MSFTNGP12.phx.gbl...
Anthony,

You could always spawn your own thread and then call Sleep on it for the specified amount of time. Does that work for you?


Nicholas,

i have done a fair amount of work with threads, even with calling Invoke on
the main forms thread and all that stuff.

try the following code for yourself and you will see what i mean:

try
{
TrySocket = new TcpClient(ServantIP, this.ServantListenPort);
}
catch
{
//do something here...
}
Even if you spawn this code in a separet thread, you will see that the main
form is blocking
anthonyb
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Derek Fountain | last post by:
Does Python have a timer mechanism? i.e. an "after 500 milliseconds, run this bit of code" sort of thing?
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...
8
by: Daniel P. | last post by:
I'm trying to set a timer that gets called every 3 seconds so I can update a field in the UI with the time elapsed since the process started. What am I doing wrong that timerDF_Tick does not get...
10
by: Bob | last post by:
Okay, I've done this for years but now I'm going to question it just because this idea has been at the back of my head since I started using DotNet... My WinForms app queries a database every 60...
7
by: hazz | last post by:
What happens if I set the timer interval for a period that is less than the time it will take to process the loop below? Right now my application is returning just a few items in an arraylist to...
4
by: Ben | last post by:
Hello everybody I got confused by this problem for which I don't have a logical explanation. There is a Thread (ThreadA) which receives Events from another system thread (ThreadS). ThreadA then...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
4
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that...
6
by: mohtasham1983 | last post by:
Hello everybody, I am trying to make a C++ timer on windows which has following functionality: Once the function is called, timer should start and last for 10 seconds, but in this period of time,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.