CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > visual_studio.visual_studio_2005 Tags:
Item Type: NewsGroup Date Entered: 7/16/2007 1:59:47 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 3 Views: 184 Favorited: 0 Favorite
4 Items, 1 Pages 1 |< << Go >> >|
pulkitzery
Asp.Net User
Timer Object Blocks Form Thread7/16/2007 1:59:47 PM

0

Hello Everyone,

 

I need some help here, I have a form on which I have all the user interface components, and I need to do a big calculation every 5 seconds. I used the Timer object to do this calculation every 5 seconds. So far so good, but my calculation takes about 2-3 seconds and because of that it made my form very unresponsive for that period of time, which is never acceptable.

 

I googled the problem and found that If I use System.Timer.Timers object It should not block the Form?s thread, because it runs on it own thread. I did that and still same results. After every 5 seconds when my Timer Click happens my form becomes unresponsive for 2-4 seconds. By googling further I found that the SychronizationObject of the timer is the property that makes sure that the timer synchronizes with that object. I changed that property to a label in my form but that did not help, may be because the label itself is in the same thread of Form.

 

Can I make a synchronization object in my code and set the property of my timer to that object? If yes, how can I do that. I no what are other way around to this problem. Can anyone help me in figuring this out?

 

By the way this is my Timer?s Properties:

            AutoReset: True

            SychronizationObject: myForm

 

Thanks in advance for your help.

 

Jerry.

 

flbas
Asp.Net User
Re: Timer Object Blocks Form Thread7/16/2007 2:24:21 PM

0

Hi

 why not use threading instead?

you would avoid probably all of these problems, and it is very easy.  i cannot post a sample here, but google it, and you will see.

basically, you will create a timer that will start the thread and do the calculation.  this will allow your form to continue on

maybe you would prefer a different method altogether. look at WCF / Silverlight for forms.  Silverlight is the Web version, but it gives you an idea of how MS is going.  WCF should allow you to do your calcuation in the background, while not locking the form "for free"

let me know...

 


Please be kind and let me know if my suggestion helps...
pulkitzery
Asp.Net User
Re: Timer Object Blocks Form Thread7/16/2007 3:00:59 PM

0

Hello, Thanks for the reply,

Actually I tried the way you suggested. But what is going on is, I need to update some user interface components on the From, at the end of the calculation. Now here are some problems I faced:

1.)    If my timer is throwing new thread every 3-4 seconds, and my calculation is not done yet when the first thread was created, and now the second one is out there and they both done about at the same time, then what happens is they clash and I get the error that the resources I am trying to use are being used by another thread.

 

2.)    Second, I want to update my screen in sequence, which means I want to update my user interface components only after the call to the calculations is done, and afterward I want to start the next call to the calculations.

 

I think timers have the capability to do all this and run as a separate thread, but I don?t know how. Hope this helps you to understand the problem. Any ideas or suggestions are appreciated. Thanks again for the reply.

 

Benners_J
Asp.Net User
Re: Timer Object Blocks Form Thread7/16/2007 4:10:15 PM

0

I think for the timer to work as you want, you need to have no synchronization object set.  That way the timer will run on a new thread.  However, starting a thread will give you better control with the timing.  I doubt you want a new calculation to begin running until the previous one has ended, so a loop running on a new thread would be ideal.  You could add a minimum time that you want between updates so if the calculation does finish quickly, it won't start again immediately.

Also, you can run into problems updating the UI from a different thread (You'll have this problem with the timer control or a new thread).  The Form (and any controls on it) should only be updated by the thread that it was created on.  You will need to use the Control.Invoke() method to update the UI.

Here is a sample that might help out.  I have two buttons.  One is called btnStart, and one is called btnStop.  You will probably want to just use the Form.Load.  Then I have a label called label1 that is being updated.  You will replace doCalculation with your calculation.

 

 // A flag to indicate if the update should continue
        private bool _ContinueRunning = false;

        // Start the calculation
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (_ContinueRunning == false)
            {
                _ContinueRunning = true;

                // Create a new thread and set is as a BackgroundThread so it will end when the app closes
                Thread calcThread = new Thread(new System.Threading.ThreadStart(runCalc));
                calcThread.IsBackground = true;
                calcThread.Start();
            }
        }

        //Stop the calculation
        private void btnStop_Click(object sender, EventArgs e)
        {
            _ContinueRunning = false;
        }

        // Method that runs the calculation
        private void runCalc()
        {
            while (_ContinueRunning)
            {
                // The date calculations and sleep ensure that the calculation is
                // only run once every five seconds.
                DateTime startTime = DateTime.Now;
                doCalculation();
                TimeSpan timeElapsed = DateTime.Now.Subtract(startTime);
                if (timeElapsed.Seconds < 5)
                    Thread.Sleep(5000 - (int)timeElapsed.TotalMilliseconds);
            }
        }

        // The calculation.  Call UpdateUI from this function with the results.
        private void doCalculation()
        {
            int delay = new Random().Next(4000);
            Thread.Sleep(delay);
            UpdateUI("Calc took " + delay.ToString() + " milliseconds to compelete");
        }

        // A delegate to update the UI on the form's thread
        private delegate void UpdateUIDelegate(string results);

        private void UpdateUI(string results)
        {
            // Check if this is running no the form's thread.  If not, invoke it on the form's thread.
            if (label1.InvokeRequired)
            {
                label1.Invoke(new UpdateUIDelegate(UpdateUI), new object[] {results});
            }
            else
            {
                // This is being run on the form's thread.  Update the UI controls.
                label1.Text = results;
            }
        }
 
4 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Professional VB.NET 2003 Authors: Bill Evjen, Billy Hollis, Rockford Lhotka, Tim Mcart, Pages: 549, Published: 2004
Programming .NET components Authors: Juval Löwy, Pages: 624, Published: 2005
Java examples in a nutshell Authors: David Flanagan, Pages: 701, Published: 2004
Practical guidelines and best practices for Microsoft Visual Basic and Visual C# developers Authors: Francesco Balena, Giuseppe Dimauro, Pages: 570, Published: 2005
Proceedings Authors: IEEE Computer Society , TC on Distributed Processing, IEEE Computer Society. TC on Distributed Processing, Institute of Electrical and Electronics Engineers, Pages: 570, Published: 1993

Web:
VB.NET: Timer Object - Blocks the Form Thread. - bytes While it may not be the problem, you do realize that processors aren't magical devices. Just because a heavy calculation is moved to another ...
17.2. threading — Higher-level threading interface — Python v2.6.2 ... By default, a unique name is constructed of the form “Thread-N” where N is a .... Otherwise, if another thread owns the lock, block until the lock is unlocked. ..... Timer Objects; 17.2.8. Using locks, conditions, and semaphores in the ...
How to block the UI thread from another thread or force a form to ... Forms.Timer ) on my form and a queue of Actions with an Tick function that ... the thread that owns its object (which will be the UI thread) and then block. ...
Vb.net: How To Trap Exceptions In Invoked Methods? Nov 17, 2007 ... HIGLIGHTS: Exception, Vbnet, Thread, Nbspnbspnbspnbspend, Invoke, Subbr, Rais, Handle, ... Forms.Timer object, the Timer class (FullName: System.Timers. .... I used to utilize the Invoke Required flow block as well. ...
Visual Basic :: Timer Control And Multi-threading In VB6 There's a timer control which transfer the data from the arrays into a DB. ..... as seperate objects) gets blocked i.e. the complete application blocks. ... As soon as a form in any thread is blocked by some time-taking task (say a for ...


Videos:
Using Static Analysis For Software Defect Detection Google TechTalks July 6, 2006 William Pugh ABSTRACT I'll talk about some of my experience in using and expanding static analysis tools for ...
Using Static Analysis For Software Defect Detection Google TechTalks July 6, 2006 William Pugh ABSTRACT I'll talk about some of my experience in using and expanding static analysis tools for ...
Using Static Analysis For Software Defect Detection Google TechTalks July 6, 2006 William Pugh ABSTRACT I'll talk about some of my experience in using and expanding static analysis tools for ...
Using Static Analysis For Software Defect Detection Google TechTalks July 6, 2006 William Pugh ABSTRACT I'll talk about some of my experience in using and expanding static analysis tools for ...
Using Static Analysis For Software Defect Detection Google TechTalks July 6, 2006 William Pugh ABSTRACT I'll talk about some of my experience in using and expanding static analysis tools for ...






unable to read project file... you must install all of the following components...

vc++ dll calling vc# and receving wm_copydata msgs

vs2005 visual sourcesafe sln file in submap

a replacement for <asp:contentpager>

visual studio 2005 to develop web sites

app_code version

how does vs.net 2005 perform remote debugging?

extensibility - uihierarchyitem path question

how do i access initilizecomponent() in vs2005?

web app error ... but no details

add project reference?

visual studio 2005 debugger times out

work items tracking from team foundation server?

file.exists returns for files on the network drive

webapplication project is not available after installing sp1.

assembly build errors

publishing in vs.net 2005

dropdownlist in detailsview

extending sqldatasource - where's the toolbox icon?

how i can show the progress bar for a time span, when i assign the datatable values to datagrid's datasource.

embedded web server vs. iis

"table does not exist error"occurs in asp.net while executing a simple query using oledb

open vs from remote station problem

adding sqldataadapter to component causes error

smarttags

lost shortcuts in vs'05

how to create a visual studio windows live plug in?

can i install jre on same machine where vs2005 is installed ?

what's changed in 2005?

enterprise template projects

   
  Privacy | Contact Us
All Times Are GMT