Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net not making sense

Posted on 2004-09-30
3
Medium Priority
?
247 Views
Last Modified: 2010-04-23
ive been looking into multi threading, and ran across this example.  what it does is makes a progress bar move back and forth while you are still able to click the buttons on the form.  

heres the code.  if you want to paste this in a blank form, and remember to import the threading header file, it should work

    Public Sub pbar()
        Randomize()

        Me.ProgressBar1.Minimum = 1
        Me.ProgressBar1.Maximum = 100
        Me.ProgressBar1.Value = 50
        Dim num As Integer

        While 1
            num = (Rnd() * 23)
            If (num Mod 2) = 1 Then
                If Me.ProgressBar1.Value > 1 Then Me.ProgressBar1.Value -= 1
            Else
                If Me.ProgressBar1.Value < 99 Then Me.ProgressBar1.Value += 1
            End If
            Me.Refresh()
        End While
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim InstanceCaller As New Thread(AddressOf pbar)
        InstanceCaller.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("test")
        End
    End Sub

as you can see the while loop is really the heart of the program, and it works perfectly.  but what i dont understand is, if you replace          

If (num Mod 2) = 1 Then

with

If ((Rnd() * 23) Mod 2) = 1 Then

the if statement always fails, and it goes strait to 100%

what i dont understand, is that it is just a simple substitution, but alas, its not.

thanks!

p.s. the number 23 is arbitrary, no rhyme or reason to that choice.

~b
0
Comment
Question by:bramsquad
  • 2
3 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12194659
Why don't you make "Button1.Enabled=False" during that time ?
0
 
LVL 8

Author Comment

by:bramsquad
ID: 12194732
i hope im not misunderstanding your answer, but i dont think thats the issue

the issue is not with it working...the issue is that it treats what seems like to similar pieces of code differently.

the value of num = (Rnd() * 23)

so

If (num Mod 2) = 1 Then

should be equivalent to

If ((Rnd() * 23) Mod 2) = 1 Then

but its not...
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 12194842
lol.. I didn't even read ur whole question...
well, why this is happening... is pretty straight forward. Use this,  ((Cint(Rnd() * 23)) Mod 2) = 1
This will explain everything. "num" is integer. So it is getting the integer value out of    Rnd() * 23.  So in order to make it work same, convert it to integer.
-Baan
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question