|
By wiring I mean that you have an eventhandler attached to the objects event. From your code I suppose that Timer refers to the object drawn to the window.
At a glance I don't see anything special in your code but all necessary operations are not included. I take it that the rest is defined in Designer.vb (for example interval and so on).
One possible problem you may encounter is that Tick isn't necessarily raised while debugging. This could be one reason why it sometimes is run and sometimes not.
The need to optimize rises from a bad design
|
|
|
|
|
Thanks for checking.
You're right, I did the form in the designer and it refers to lblTimer - which is nothing than kind of a stopwatch - drawn to the window. After building an exe and starting it several times, I have to say that it's exactly the same effect: Sometimes it starts counting (one or two seconds was the maximum), sometimes the label just stays at 0.
Would it be possible that the Backgroundworker blocks the timer... something like 'race conditions'?
modified on Sunday, September 28, 2008 1:00 PM
|
|
|
|
|
For a race condition you should use a common resource in both threads. And even if that's true, it should appear when you use the resource, not on timer tick.
However, it's possible that you are blocking the whole UI thread (I take it that the timer is in main UI thread) and in that case the tick won't occur. If that's true your UI should also be non-responding.
There's also the possibility that the timer's enabled property goes to false. Try using debugger and add a conditional breakpoint to timers enabled property and define the debugger to stop every time the value changes. This helps you to eliminate that possibility.
The need to optimize rises from a bad design
|
|
|
|
|
Hi Mika,
thanks for still taking care.
Mika Wendelius wrote: The need to optimize rises from a bad design
I have to admit that being a newbie I seem to fall into almost every bad design trap I can find
What I did meanwhile is that I eliminated the timer as well as the Backgroundworker. Instead of that, the looping function (which used to be in the background worker) calculates the time elapsed within every loop and updates my lblTimer label. This way might not be elegant (and I still didn't learn how to use the Timer right) but it works for my small purpose and I see how much time the function needed.
Mika Wendelius wrote: However, it's possible that you are blocking the whole UI thread (I take it that the timer is in main UI thread) and in that case the tick won't occur. If that's true your UI should also be non-responding.
I had started the timer in the main UI thread of a form, in which (I guess) also the Timer_Tick event runs (I placed the component in the form). The update of the lblTimer label was supposed to come from the Timer_Tick event (after starting / before ending the timer) every second and independently from the looping stuff done in the backgroundworker (that was the idea). Other controls (a progressbar and another label), which are updated in the backgroundworkers ProcessChanged event, do update with every loop!
Do you think it's possible that the BW takes so much of the processor time that it doesn't leave any for the main thread? Somehow that doesn't make sense for a backgroundworker, does it?
|
|
|
|
|
Oops,
That optimizing thing is only my signature. I think I should remove so that people won't take it badly. It certainly wasn't targeted to you! Sorry for that.
If you're reading or updating lblTimer in background worker, you may block the timer (or actually the UI) and it can cause you several other problems since UI elements must be updated only in UI thread. Also if the backroundworker continuously uses the lblTimer, it may cause a collision with the timer in event handling.
Just a question: Why don't you update the elapsed time in Timer_Tick instead of using backgroundworker? It could simplify the code a lot. For what you described I don't see the benefit of using backroundworker. Another possibility is to use two separate timers in which case they would still be under the same thread, but I'm not sure that iven that is needed
Mika
The need to optimize rises from a bad design
|
|
|
|
|
Don't mind your signature it's a good reminder to learn, anyway, and I didn't take it personally.
Something you misunderstood: I didn't update lblTimer (a label on the main form) in background worker, but in the Timer_Tick event. Other controls (progress bar etc.) were updated in background worker (process_changed).
The use of a background worker arose from the idea NOT to block the UI with long lasting (looping) function (purpose: listing of large directories in a given path). So my basic concept for that seemingly simple thing was:
1. Start the timer from the forms main routine
2. Start a background worker (also from the forms main routine)
3. Let the timer count the seconds while user sees a marquee-style progress indicator as long as the background worker is running
4. Stop timer when background worker is finished
Actually I tried out the whole BW / Timer stuff to learn something about better design but it seems I'm trapped somewhere
|
|
|
|
|
Ok,
As far as I can tell, the basic idea seems good. And if the backgroundworker isn't accessing any UI elements directly, you should be safe. However, if the timer and the BW share some variables, arrays etc, you should lock them before accessing (not sure what is the keyword in VB but in C# it's lock ).
Perhaps if you strip the code to the minimum and then try bringing it back piece by piece, you'll find out what went wrong.
The need to optimize rises from a bad design
|
|
|
|
|
If at least my basic idea was ok, then I might give the timers and background workers another chance
Still it might have been the problem that my Timer_Tick event was supposed to change ONE element (lblTimer) on the form (but didn't) while background worker changed OTHER elements on the same form (and did!). No similar variables, but still accessing the same form.
Thank you very much for your help, Mika!!!
Regards
Michael
|
|
|
|
|
You're welcome
I believe that this can be useful to you if you haven't read it already:
Control.InvokeRequired Property [^]
Mika
The need to optimize rises from a bad design
|
|
|
|
|
I would just blame all your timer problems on the financial market - kidding!
Any suggestions, ideas, or 'constructive criticism' are always welcome.
|
|
|
|
|
The financial market already blamed the timer problems
|
|
|
|
|
hi all ,
i want to print the values as crystel repots
Regards
Vidhish
|
|
|
|
|
I want a Tesla. clickety[^]
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello everybody,
before I go mad with all these error messages, I want to ask someone who knows... How can I simply shorten a path with regular expressions, just the way Windows does it in many dialogs?
Input:
C:\program files\Visual basic examples\GMTest\Pictures\pic00001.jpg
Output:
c:\program files\....\pic00001.jpg
In words: The output string contains the start input sting until the 2nd "\", then followed by "...", and the end of the new string starts with the last "\" of the input string.
re = New Regex("^(?'start'.*\\.*)\\.*\\(?'end'\w*)$")
tmpText = re.Match(Text).Result("${start}...\${end}") does quite a good part of the job but still unsatisfying.
I've been trying to figure out regular expressions for hours already, but now I'm stuck and can't get a reasonable result. Could anybody help, please?
Thank you,
Michael
|
|
|
|
|
Finally - after ages - I can answer my own question as an inspiration for anyone else with a similar problem:
re = New Regex("^(?<start>\w:\\[\w\s]+\\).*\\(?<end>.*)$")
tmpText = re.Match(Text).Result("${start}...\${end}")</end></start> returns exactly the desired output string! Very useful e.g. for indicators when looping through a directory structure.
Regards
Michael
|
|
|
|
|
Do take a look at the Expresso tool on this site. It is helpful when creating/testing regular expressions.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Thank you for the hint - just downloaded it. Even after reading the description only, I guess the tool might have helped saving many of my nerves during the last hours...
Have a nice day!
|
|
|
|
|
Michael Schäuble wrote: Thank you for the hint - just downloaded it.
No problem. I think the author has other stuff on reg expressions that are good too. Not 100%, just getting fuzzy now from
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Iam using Icomparer property to sort the list in Generic Functions
stlist--is a class and I used generic functions (like entity collections for a table)
stcomparer is a class used in stlist and i wirtten icomparer function it it
Public Class stcomparer
Implements Generic.IComparer(Of stlist)
Private m_SortProperty1 As PropertyDescriptor
Private m_SortDirection1 As ListSortDirection
Public Function Compare(ByVal x As stlist, ByVal y As stlist) As Integer _
Implements System.Collections.Generic.IComparer(Of stlist).Compare
Return x.ss_issuedate.CompareTo(y.ss_issuedate)
End Function
StiList.Sort(0, StiList.Count, New Stcomparer)
it is sorted By ss_date in ascending order..here I have hard coded it..
Is ther any way to pass the filed in Icomparer and sort according to it??
Can i change the Icomparer PEROPERTY parameters
|
|
|
|
|
cbenan wrote: Is ther any way to pass the filed in Icomparer and sort according to it??
Can i change the Icomparer PEROPERTY parameters
What are you getting at? What is it you're trying to accomplish with this?
|
|
|
|
|
How can i create a application for hiding Process from Task Manager.
If you any idea Please help.
Thanks
Anubhava Dimri
|
|
|
|
|
Asking the same question again will neither change the answer, not get you another answer any faster.
|
|
|
|
|
Dave Kreskowiak wrote: Asking the same question again will neither change the answer, not get you another answer any faster.
Very true.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
The answers haven't changed in the 3 hours that passed since you asked this the first time.
|
|
|
|
|
Why post again, when you just got some quality replies?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|