Solved

vb.net not making sense

Posted on 2004-09-30
3
202 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now