|
-
Sep 15th, 2007, 01:34 PM
#1
Thread Starter
Fanatic Member
[2005] Form and showing a animation
Hi!!
In my project i added 16 images that form a animation. Then i added a form, in which i added a picture box (Circulo) and a Timer (Timer1).
Then i added this code:
Code:
Dim ImagemActual As Short = 0
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Imagens(15) As Bitmap
Imagens(0) = My.Resources.Posicao1
Imagens(1) = My.Resources.Posicao2
Imagens(2) = My.Resources.Posicao3
Imagens(3) = My.Resources.Posicao4
Imagens(4) = My.Resources.Posicao5
Imagens(5) = My.Resources.Posicao6
Imagens(6) = My.Resources.Posicao7
Imagens(7) = My.Resources.Posicao8
Imagens(8) = My.Resources.Posicao9
Imagens(9) = My.Resources.Posicao10
Imagens(10) = My.Resources.Posicao11
Imagens(11) = My.Resources.Posicao12
Imagens(12) = My.Resources.Posicao13
Imagens(13) = My.Resources.Posicao14
Imagens(14) = My.Resources.Posicao15
Imagens(15) = My.Resources.Posicao16
Circulo.Image = Imagens(ImagemActual)
Circulo.Update()
ImagemActual += 1
If ImagemActual = 15 Then ImagemActual = 0
End Sub
With this code when i hit play, the animation in the form works perfectly. But if i add a line like this:
Code:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Timer1.Start()
UpdateDB()
End Sub
UpdateDB is a Sub that does a lot of stuff (opens a file, then changes a DB according to the file)
If i add this line he does the intire code of UpdateDB() and then he show ONLY the first image of the animation, like the timer isnt working. Can u help me so that he does the code in UpdateDB() and at the same time he shows the animation. If I put Timer1.Start() at the start of UpdateDB() sub its like it is the last thing he does, has if it as on last.
Last edited by Lasering; Sep 15th, 2007 at 01:41 PM.
-
Sep 15th, 2007, 09:00 PM
#2
Re: [2005] Form and showing a animation
A thread can only do one thing at a time. If your UI thread is busy updating your database then it can't be doing anything else, so all your Tick events get queued until the database update is finished.
The dodgy way to overcome this is to call Application.DoEvents occasionally in your UpdateDB method. This will process any pending events and thus keep your animation going. It will only be so effective though, as you cannot guarantee that several Tick events will not stiil be queued, thus making your animation jerky.
The "proper" way to do this is with multi-threading. You could create a new thread and set UpdateDB as its entry point. Alternatively you could use a BackgroundWorker component to simplify the job. Your UI thread is then free to handle the Tick events while the worker thread does the grunt work. Just note that if you do this and your UpdateDB method assesses any controls, you will have to adjust your code to ensure that that gets done on the UI thread. That means doing so in the BackgroundWorker's ProgressChanged and/or RunWorkerCompleted event handlers, or else using delegation to explicitly marshal the call to the UI thread.
-
Sep 16th, 2007, 02:54 PM
#3
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
What do u mean with thread? and its entry point?
The rest i understand it!
-
Sep 16th, 2007, 03:02 PM
#4
Re: [2005] Form and showing a animation
-
Sep 16th, 2007, 04:38 PM
#5
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
Let me see if i understand it well: my UI thread is the one that deals with the ticks, the one where i put the line Timer1.Start() in this case would be the UpdateDB() sub. The worker thread (BackgroundWorker1_DoWork) would be the one where the "main code" (the one i want to run simultaneously with the timer) and in the UI thread i also would have to put BackgroundWorker1.RunWorkerAsync(). Is this right?
-
Sep 16th, 2007, 05:16 PM
#6
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
i've been reading somethings and now i'm completly confused.
Can someone give me a simple example of multithreading?
-
Sep 16th, 2007, 06:23 PM
#7
Re: [2005] Form and showing a animation
In principle, multi-threading is extremely simple. A thread is a basically a space inside which a series of operations get executed. Inside one thread, only one operation can be executed at a time. By default your application consists of a single thread, so your application can only do one thing at a time. If you tell it to perform some time-consuming operation then it will be too busy to refresh the UI for that period of time. It will also be too busy to handle incoming events as they happen so they must be queued to wait until the thread is available. For this reason single-threaded applications can become unresponsive at times.
Now, the general idea of multi-threading is that if you have some long-running operation to perform you create a new thread (or threads), which is a new space inside which this long-running operation can execute. This leaves your app's main thread free to refresh the UI if needed, and also to handle any incoming events as they happen.
A thread has to start somewhere. The first method that gets executed inside a thread is called its entry point. In .NET you implement multi-threading by creating a Thread object, specifying its entry point and then calling its Start method. This will begin a new thread executing at the specified entry point.
The BackgroundWorker is a new component added in .NET 2.0. It is NOT a thread. The BackgroundWorker is a component that hides much of the complexity of multi-threading from the developer, thus making its implementation easier. To use a BackgroundWorker you simply call its RunWorkerAsync method and handle its DoWork event. That act of creating the worker thread is done for you. The BackgroundWorker starts a new thread and then raises its DoWork event on that thread, thus your DoWork event handler is executed on the worker thread.
One of the tricky things with multi-threading is that you cannot access controls directly from the worker thread. Generally you must use delegation, which is not really all that difficult but does increase the complexity. The BackgroundWorker eases this pain as well. You can call the ReportProgress method from the worker thread and this will raise the ProgressChanged event on the UI thread, thus you can access controls directly from the ProgressChanged event handler. Once the DoWork event handler completes the BackgroundWorker has finished its work. At that point it raises the RunWorkerCompleted event, also on the UI thread. This makes it easy to update the UI to reflect the new state.
Note that the BackgroundWorker also eases the job of passing data into and out of the worker thread. You can pass an object parameter to RunWorkerAsync and get this back via the e.Argument property in the DoWork event handler. You can pass an object parameter to ReportProgress and get this back via the e.UserState property in the ProgressChanged event handler. You can also assign an object to the e.Result property in the DoWork event handler and get this back via the e.Result property in the RunWorkerCompleted event handler.
The BackgroundWorker is not suitable for all multi-threading scenarios but the majority of cases are relatively simple and would benefit from the use of a BackgroundWorker. For a code example see here. For more information on managed thread I strongly suggest that you read this.
-
Sep 17th, 2007, 11:20 AM
#8
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
I've tried like u said with the background worker, but the animation isnt uniform.
Now i go try with the multithread.
-
Sep 17th, 2007, 04:33 PM
#9
Re: [2005] Form and showing a animation
Um, if you're using a BackgroundWorker then you are already using multi-threading. Like I said, the BGW just does some of the work for you to make multi-threading easier.
If you're animation is not completely smooth then it's probably because your background task is very processor intensive. If you have a single processor core then your machine can still only do one thing at a time. It will interleave different threads to create the appearance of multiple tasks occurring at the same time but for each block of execution time a processor can still only execute one thread.
-
Sep 19th, 2007, 07:09 AM
#10
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
The background task is:
open a file and read it, then replace in a DB in the right tables thet values that are different.
So the best i can do is to take off the animation.
-
Sep 19th, 2007, 09:26 AM
#11
Re: [2005] Form and showing a animation
You could set the Priority of the background thread to BelowNormal so more time is devoted to the UI thread, but then your background task may take longer.
-
Sep 23rd, 2007, 05:43 PM
#12
Thread Starter
Fanatic Member
Re: [2005] Form and showing a animation
it could work. But the problems doesnt worth the effort. It has more for better look then anything else, but i learned how to use background worker from it.
By the way thks for ur answers very very helpfull.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|