Solved

vb.net not making sense

Posted on 2004-09-30
3
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 125 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - String Manipulation Scenario 2 45
vb.net background worker crossthread 11 45
Import a excel sheet in a grid 2 45
wrapping text in datagridview header columns 1 30
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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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