Recent

Author Topic: strange resources error (only 1 resource file can be extracted)  (Read 7672 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
strange resources error (only 1 resource file can be extracted)
« on: October 27, 2011, 01:13:21 pm »
Hallo. Please , download and test attached project. When I run it and click on button1 I see error:
http://img62.imageshack.us/img62/6275/62399579.png

But when un-comment 57-line there is no error. Why? And what sleep time is optimal? Mayby sleep time depends of user computer characteristics (RAM, processor clock frequency). If so, how I know it on development stage ?
« Last Edit: October 28, 2011, 10:16:42 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: strange resources error (only 1 resource file can be extracted)
« Reply #1 on: October 27, 2011, 02:41:21 pm »
Well, I don't want use sleep() at all, because argument (time) is unknown. So please help to find out failure cause.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: strange resources error (only 1 resource file can be extracted)
« Reply #2 on: October 27, 2011, 05:24:46 pm »
You have to comment Randomize; in SaveResourceAsTempFile() function. The seed will be the same for both files because 1 call to the function takes less than 1 millisecond, it doesn't have enough time to change. Randomize should never be called more than once per whole application.

Actually, another way would be creating private boolean variable for unit called "IsRandomized" that would start as false. First call to SaveResourceAsTempFile() would set it true so it wouldn't be randomized again.
« Last Edit: October 27, 2011, 05:28:56 pm by User137 »

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: strange resources error (only 1 resource file can be extracted)
« Reply #3 on: October 27, 2011, 05:41:08 pm »
You have to comment Randomize; in SaveResourceAsTempFile() function. The seed will be the same for both files because 1 call to the function takes less than 1 millisecond, it doesn't have enough time to change. Randomize should never be called more than once per whole application.

Actually, another way would be creating private boolean variable for unit called "IsRandomized" that would start as false. First call to SaveResourceAsTempFile() would set it true so it wouldn't be randomized again.

A-a-ah! So if time is  exactly known and hardware-insensitive, I can just add sleep(1) , cannot I?

Update:
Nope! Even sleep (20); doesn't help and I see error. So randomize and seed neither here nor there
« Last Edit: October 27, 2011, 06:19:57 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: strange resources error (only 1 resource file can be extracted)
« Reply #4 on: October 27, 2011, 05:59:40 pm »
Sleep(2) would be more secure, and sleep() as function is not accurate, it can possibly vary alot depending on OS. Besides random() in theory can produce 2 same numbers in a row.

Furtherhermore if you always keep calling Randomize the numbers generated will not be truly random. You call Random() 9 times in the function, so at the end of it Randseed has grown by 9. If you temporary-fix it with sleep(1) the Randseed goes backwards to be increased just 1 from last time.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: strange resources error (only 1 resource file can be extracted)
« Reply #5 on: October 27, 2011, 07:54:14 pm »
You was right. Commenting randomize out helps.

THANK YOU VERY MUCH!  (^_^)

(^3^)
« Last Edit: October 28, 2011, 06:41:35 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: strange resources error (only 1 resource file can be extracted)
« Reply #6 on: October 27, 2011, 09:57:46 pm »
Never call randomize twice in a program. You will initialize the pseudo random stream of values at the same position, if it happens in the same miliseconds.

Calling it twice, doesn't make the sequence more random.
« Last Edit: October 27, 2011, 10:10:04 pm by Vincent Snijders »

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: strange resources error (only 1 resource file can be extracted)
« Reply #7 on: October 28, 2011, 06:02:13 am »
Never call randomize twice in a program. You will initialize the pseudo random stream of values at the same position, if it happens in the same miliseconds.

Calling it twice, doesn't make the sequence more random.
Do you think that's so good?
It's a bug because in this page there is no info about once - twice randomize in one app.
So you suggest to use randomize once , to prevent bug disclosure.

Secondly, random func gives non-real randoms anyway, no matter once, twice , thrice, fourfold etc. randomize exist.
« Last Edit: October 28, 2011, 06:21:21 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: strange resources error (only 1 resource file can be extracted)
« Reply #8 on: October 28, 2011, 07:32:22 am »
And if you use the same seed or call randomize in the same milisecond (which is uesd as seed of randomize), then the sequence of random numbers are the same.


anna

  • Sr. Member
  • ****
  • Posts: 426
Re: strange resources error (only 1 resource file can be extracted)
« Reply #9 on: October 28, 2011, 10:16:28 am »
And if you use the same seed or call randomize in the same milisecond (which is uesd as seed of randomize), then the sequence of random numbers are the same.
Thanks.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

 

TinyPortal © 2005-2018