Timer Issue

agraham

Expert
Licensed User
Longtime User
I'm afraid that it is no use trying to describe what is happening. It doesn't give me enough information, I would need to see the structure of the actual code to understand what you are saying.

I've tried my Progress2.sbp under v6.50 and there is no problem with a Panel. The program is attached, see if it works for you.
 

Attachments

  • Progress2v6.50.sbp
    1.3 KB · Views: 192

TWELVE

Active Member
Licensed User
Your example is working here as well.The difference is, that you setup the thread and the timer, and then leave the BtnStart_Click sub.In my code the sub cannot return yet because the jpeg.loadthumbnail is still running.

See the attached file...


regards,

TWELVE
 

Attachments

  • Progress2v6.50_twelve.sbp
    1.4 KB · Views: 182

agraham

Expert
Licensed User
Longtime User
This is your problem
B4X:
   Do While Thread1.Running = True
       Sleep(10000)
   Loop
You are looping within your button Click event and so the application never gets back to the message loop and so cannot run timer events. I don't understand why you think this is necessary! And sleeping for 10 seconds means the GUI is going to be frozen for at least that length of time. If you really think you must wait for the operation to finish then do this.
B4X:
   Do While Thread1.Running = True
       DoEvents
       Sleep(40) ' same length as the progress update timer
   Loop
 

TWELVE

Active Member
Licensed User
You are looping within your button Click event and so the application never gets back to the message loop and so cannot run timer events. I don't understand why you think this is necessary! And sleeping for 10 seconds means the GUI is going to be frozen for at least that length of time. If you really think you must wait for the operation to finish then do this.

I understand, but the sleep() was only a placeholder.While the jpeg.loadthumbnail is running i see same behavior...

regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
Where are you putting the call to Jpeg.LoadThumbnail? This is really so simple I can't understand your problem, especially as you won't post the actual code.

B4X:
Sub BlockingSub
 Jpeg.LoadThumbnail
End Sub
...

Sub WaitForThumbnail
  Thread1.Start("BlockingSub")
  Pbar1.Value = 0
  Timer1.Enabled = True
  Panel1.Visible = True
  Do While Thread1.Running = True
  DoEvents
    Sleep(40) ' same interval as the timer
  Loop    
End Sub
 

TWELVE

Active Member
Licensed User
I think i got it...for some reason i don't know the panel.visible and timer.enabled have to be executed outside of the the thread.

This is, was my code does...the thread is started and brings then up my waiting box.Because i don't want to see the panel all the time, i use some more commands like panelxx.bringtofront to manipulate the panel.

This gives some unexpected behavior, once the panel appeared one time ( with progressbar that does not update..), it will not appear a second time if the thread is called again.

If i move the code that brings up the waitingbox outside of the thread, everything works fine.

I was a bit kind of in a "loop", because without threading the bar did not update for known reasons ( jpeg.loadthumbnail blocked everything) and then i just started my sub as thread, which does not work for the panel and pbar - for some unknown reasons.

My intention was to show this waiting box to the user whenever it is needed, but that does not work from within a thread, so i need to redesign some code parts to make it possible even so.

See the attached modified example...


regards,

TWELVE
 

Attachments

  • Progress2v6.50_twelve2.sbp
    1.4 KB · Views: 199

agraham

Expert
Licensed User
Longtime User
then i just started my sub as thread, which does not work for the panel and pbar - for some unknown reasons.
You must only access GUI objects from the main thread. You must not access them from any other thread that you start. That is why I have provided thread events. Those events are run on the main thread and can update GUI elements if required but they are of no use when the thread is running a blocking call like LoadThumbnail.
 

TWELVE

Active Member
Licensed User
Andrew, thank you very much for pointing this out..;-)

I already use the Thread_ThreadEvent in my other thread, but apparently forgot about it...i'm sorry that it took that long but on the other hand i learnt again something more.


regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
I am actually a bit puzzled why trying to enable a timer in the new thread doesn't work. I haven't noticed that effect before but as I don't know in detail how a timer is implemented I can't explain it. I guess it is something to do with messages and the message loop.
 

TWELVE

Active Member
Licensed User
I'm still struggling with putting the jpeg resizer in a thread.On desktop everything runs fine, but on the device i get exceptions indicating that the thread tries to access a control that is under the main gui control.After putting in much effort i threat to fail

I use this somehwere in the main code:

AddImage ("Form1", "Image1", 80,80,100,100)
Image1.Visible = False


because i need the image control for this:

Image1.Image = Jpeg.LoadThumbnail(UploadImageFileName, ScaleToWidth, ScaleToHeight, True)
Jpeg.Save(Image1.Image,UploadImageTempFileName , 80)

I attempted to move the AddImage into the thread, but then i get an exception, that i cannot assign controls i created within a thread to a parent control in a different thread.

The code execution appears to be way more strict on device, so didn't face the exception before i compiled a version for the device, on desktop was everything fine, at least in run mode.

How can i get out of this...?



regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
The device does indeed makes checks for cross-thread access that the desktop doesn't do. You can only add a new control on the main thread. You could try loading the Thumbnail into a new Bitmap that is not assigned to an Image and then add the Image control and assign the Bitmap to it in a ThreadEvent.
 

TWELVE

Active Member
Licensed User
You can only add a new control on the main thread.

That's what i did...it's complaining that the form i will be assigning the image control to is under the main thread control.To be honest, i did not really understand what you were trying to tell me...

Can you work out that a little bit more for me..? Not really sure what you mean by a "bitmap"..? Does it invoke a bitmap object like from your ImageEx lib...?


I found this one worked and is currently my only solution to that approach:

Jpeg.Save(Jpeg.LoadThumbnail(UploadImageFileName, ScaleToWidth, ScaleToHeight, True),UploadImageTempFileName , 80)

It is just a shortcut for:

Image1.Image = Jpeg.LoadThumbnail(UploadImageFileName, ScaleToWidth, ScaleToHeight, True)
Jpeg.Save(Image1.Image,UploadImageTempFileName , 80)


The outcome is that i don't need the image control at all that way.Of course there's some more disk activity, because it is reading and saving at the same time, but overall it shouldn't take that longer if not even done in the same time compared to first loadthumbnail and then save later on the image control.

That is doing exactly the way i want, but i am still interested in that way you outlined in your last answer...


regards,

TWELVE
 

agraham

Expert
Licensed User
Longtime User
Does it invoke a bitmap object like from your ImageEx lib...?
Yes, a BitmapEx.

In the thread Sub

Bmp1.New4
Bmp1.Value = Jpeg.LoadThumbnail(UploadImageFileName, ScaleToWidth, ScaleToHeight, True)


In the ThreadEvent which runs on the main thread

Image1.Image = Bmp1.Value;
 
Top