Multithread, loop, sub issue please help !

dnhype

New member
Joined
Sep 24, 2010
Messages
4
Programming Experience
Beginner
Hi there,

I can't figure how to make my program work using thread, loop and sub..
Ill explain

I make web application to automate work.

Function/Sub i have so far

-Login
-Update Info
-Logout

And this should loop infinite
I want to have a function or sub for my login so i dont have to re-type all code if i need to call it later in other sub/function.

Now my problem

How to make this loop properly

Call login, wait for it to be logged or return true, update info + update UI , Logout, Loop infinite

All this using webbrowser and thread.

VB.NET:
Public function Login() as boolean

Do stuff....
Verification...
We'r loged then

Login = True
return Login

end function

Public sub UpdateUI(byval Information as string)

form1.textbox1.text = Information

end sub


public function Logout() as boolean

do stuff for logout,,

verification
if logout then
logout = true

return Logout

end function


Sub Mythread()

do
application.doevents
loop until login = true

UpdateUI(someinfo) <-- This update UI so needs some Delegate or Invoke not sure ...

do

do
application.doevents
loop until logout = true

end sub

Then my button to start thread

button1 sub ...

dim thread as new thread
thread = new thread(address of Mythread)
thread.setappartementstate = sta ( for webrowser it need this state )
thread.start

loop until X = Y 'here is the infinite loop


i type this code in notepad so it's note the real code but its how it flow.

is this a good way ? i cant make it work im getting mad !

The way i managed to make it work is in Single Thread application, having each sub / function the entire code flow ( login, update info, logout is not 1 sub )
but more function i have they all need a login function so i copy past the long code in every sub which make my apps freaking LONG im getting lost in the code. and if i need to update the login stuff i need to do it everywhere...

please help someone
 
I'm not following you here, why would you want to do multithreading in a web app? The page can't be rendered to the client while the thread is running, it makes no sense. Can you perhaps clarify this?

Also, you can split code up into multiple files, even modules. You could put the login/logout routines in a module then call them from whatever page(s) you need to.
 
I'm not following you here, why would you want to do multithreading in a web app? The page can't be rendered to the client while the thread is running, it makes no sense. Can you perhaps clarify this?

Also, you can split code up into multiple files, even modules. You could put the login/logout routines in a module then call them from whatever page(s) you need to.

You are right when you ask why multi thread, the reason i need is cause once page is loaded, i need a second thread to start doing calculation from the html source of the page.

When i use module im not able or dont find a way to make a thread update UI. i ends up having tons of sub/function into the Form1 code.

Main issue is to have many sub/function call into a sub.. the routine or flow
 
Here's is how i imagine the flow.


form1 code
VB.NET:
Public Class Form1
    Dim Thread As New Threading.Thread(AddressOf main)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Thread.Start()
    End Sub
End Class

In a Module:

VB.NET:
Module Module1

    Public Function Sub1() As Boolean
        Sub1 = True
        Console.WriteLine("sub1")
        Return Sub1
    End Function

    Public Sub UpdateUI()
        Console.WriteLine("update ui")
    End Sub

    Public Function Sub2() As Boolean
        Sub2 = True
        Console.WriteLine("sub2")
        Return Sub2
    End Function

    Sub main()
        Do
            Application.DoEvents()
        Loop Until Sub1() = True
        Console.WriteLine("sub1 compled")
        UpdateUI()
        Do
            Application.DoEvents()
        Loop Until Sub2() = True
        Console.WriteLine("sub2 compled")
    End Sub
End Module

Is that a good programming way for the flow ?
I tested and it work fine as function are quik simple and short.
However my program have way more complexe sub/function so i'd like to know if it either the way i call them or the sub it self need improuve :)

Thanks you.
 
What you're asking for isn't really possible with a WebBrowser. As it's a control, anything you do with it must be done on the same thread that owns the form. If you want to use multiple threads then you would have to create a form containing a WebBrowser control on each thread. Each form and WebBrowser control would then behave as though it was the only form in the app.
 
Back
Top