threading question...

Rothariger

Regular
Joined
Apr 7, 2004
Messages
66
hello, i have a question of threads, im making some tests, and i want to get a value of a variable inside a thread, but i want to retrieve from outside of it..
was it clear?



Code:
       'when i clic the button its start the thread. completes a variable in the class, and starts.
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Class1 As New TPruebas
        Dim myT As New Threading.Thread(AddressOf Class1.myThread)
        Class1.myInt = 100
        myT.Start()
    End Sub

    Public Class TPruebas
        Public myInt As Double
        Public incInt As Double
        Public Sub myThread()
            For incInt = 0 To myInt * 1000
                Thread.Sleep(250)
                'I put the values of the for in a textbox so i know that the thread is running.
                txtCount.text = incInt
            Next
        End Sub
    End Class

'now when i push this button, i want that this sub, retrieves the values from the thread, all my trys where fails. some one could tell me, how to do it, and if this is posible?
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As      System.EventArgs) Handles Button2.Click

    End Sub


thanks to all...
 
A thread doesnt care about class boundaries, so if you store a value in a member variable of a class in one thread, it can be used on another thread.

First you'd have to change button1_click. Instead of using a local variable myT in the function, that variable has to be defined at the class (Form in this case) level. This allows you to refer to the same TPruebas object in the button2_click method, where you can then just get the value of incInt from the TPruebas object.

I'm not sure but I think that VB.net is the same about doing GUI stuff as c#: you can't change the property of a button/textbox/any other user control, when not on the right thread. You can check this by using txtCount.InvokeRequired, if it returns true, you'll have to do a some more work to set the text. Also, if you run the programmer by pressing F5 in visual studio, you can put a break point in the for loop so you can see it starts to run as the debugger will halt at that breakpoint. This would actually be a lot simpler than using the InvokeRequired thing ;).

One final remark I can't stop myself from making. Right now the incInt can be both read (by button2_click after changing it) and updated (in the for loop) at the same time. If both are done at the same time, you could end up in a situation where the write is half done when you do the read, giving very strange results. Look into synchronization like the System.Threading.ReaderWriterLock to protect against simultanious read / write. I know VB has a build in statement you can use instead of that readerwriterlock, but dont know the exact syntax of it ;).
 
Wile said:
I'm not sure but I think that VB.net is the same about doing GUI stuff as c#: you can't change the property of a button/textbox/any other user control, when not on the right thread.

MSDN has oodles of info on multithreading, components, and controls.

MSDN(ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskmanipulatingcontrolsfromthreads.htm) said:
Multithreading is best suited to running processor-intensive procedures of class modules. Unlike other components, there are issues with directly calling methods in controls from separate threads. Methods that affect controls should be executed only on the thread on which the control was created. Because marshalling calls from one thread and sending them across thread boundaries to another is very expensive in terms of system resources, you should avoid repeatedly making calls to controls on other threads. At best, direct calls from other threads are expensive, and the performance of your application will suffer. At worst, conditions could occur that cause a deadlock in your application, freezing execution.

However, there might be occasions when you want to call methods of controls from your threads. For example, you might call a method that disables a button or updates a display on a form in response to action taken by a thread. The .NET Framework provides methods that are safe to call from any thread for invoking methods that interact with controls owned by other threads. The Control.Invoke method allows for the synchronous execution of methods on controls, whereas the Control.BeginInvoke method initiates asynchronous execution. To use these methods, you must declare a delegate with the same signature as the method you will be invoking. Then you can call the Invoke or BeginInvoke method of any control on the form by supplying the appropriate delegate to the method you want to call. Any required parameters are wrapped in an Object and transmitted to the method.
 
ok, i will explain why i want to retrieve the data from the middle of the task...

i must to make a page, that updates data a lot of data, and must to show a progress bar, but only if the user refresh the page, i dont must make it in real time, because it a lot of traffic to the server, the i make it, when the page request the info, then, when the task is running, i must to catch that task, and get the value, thats why i cant make the myT more global, because, i will initializate when i refresh the page...

i dont know if it was clear...

ex, i enter in the page, put the job running, after 5 minutes, refresh the page, and want to see the progress that the job have.
then, the only way i think, is to catch the task, and get the value of incint.

is it clear? is there any way of do it?


thanks for all the help do you give me...


i was making the tests in windows form, but latelly i must to move it to asp.net, but first i want to get this problem solved...


thankssss!!!!
 
You want to do something like this for a web page (asp.net)?

I have no experience with using threads and asp.net but I can imagine there are a lot of complications that make it even harder than using a real client.

My guess is that you need some kind of back-end server that performs jobs and allows the asp.net page to read the status of each job. Somehow the client must have an identification that points to its job(s) that are running so you can retrieve that status of that particular job.
But I have no idea how to pull something like that off, never worked with asp.net before, sorry :(.
 
Back
Top