Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BeginInvoke Status

Status
Not open for further replies.

highwingers

Programmer
Jan 29, 2004
15
US
I am using BeginInvoke to get a thread from ThreadPool, it all works well, however I need to show an indicator on GUI, that process has finished or something.

like update a Label control... or maybe via javaScript?

here is code... ( look at sub ok() )

Imports System.Threading
Imports System.Runtime.Remoting.Messaging
Partial Class Delegates_Default2
Inherits System.Web.UI.Page
Private Delegate Sub iLoop(ByRef i As Int32)
Public ustr As String = "Khan"


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load




Dim a As iLoop

Dim ar As IAsyncResult

a = New iLoop(AddressOf Looper)

ar = a.BeginInvoke(0, New System.AsyncCallback(AddressOf imDone), Nothing)







End Sub


Private Sub imDone(ByVal ar As IAsyncResult)

Dim objResult As AsyncResult
Dim mDel As iLoop
Dim myI As Int32

objResult = CType(ar, AsyncResult)

mDel = objResult.AsyncDelegate




mDel.EndInvoke(myI, ar)

System.Diagnostics.Debug.WriteLine(myI & ":Callback Finished")

Me.ok(ustr) '< --------------

End Sub

Private Sub Looper(ByRef i As Int32)

Dim availableThreads As Int32
Dim b As Int16

For j As Int32 = 1 To 30
System.Diagnostics.Debug.WriteLine(j & ":Callback Started")
Next

ThreadPool.GetAvailableThreads(availableThreads, b)



System.Diagnostics.Debug.WriteLine(i)


System.Diagnostics.Debug.WriteLine("This Value:" & i & ":avail Threads:" & Thread.CurrentThread.IsThreadPoolThread.ToString() & ":" & availableThreads & ":Current Thread" & Thread.CurrentThread.GetHashCode())

End Sub



Private Sub ok(ByVal d As String)

' I AM DONE, Show something to User

End Sub


End Class
 
The difficulty with using Threading in a web application is that it isn't like a windows application where you can just update the GUI at any time. A request is made and HTML is sent back to the sender and that is it.

To be able to implement something like this, you'll need to use AJAX based methods that send requests at various intervals from the client to the server requesting the current status. Depending on the status returned you would then update your page accordingly.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
ca8msm,

Thanks for your reply. based upon my code given above, can you modify it just a little bit so I can see where do i need to implement Ajax Calls.

Thanks
 
research what ajax is and how it works. I would also recommend researching how http and asp.net work in general. this will shed some light on why your current approach doesn't work.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
plzzzzz gimme an example... i know ajax and how http works.
 
1. using ajax send a request to start the process
2. update the ui to indicate the process started
3. server: while processing the request from #1 track the process, either percentage or state object, whatever. you will need a common storage place for this, database, asp.net session, etc.
4. client: send ajax requests to the server to get the current status. display this status to the user. place an ajax call in a timer to poll for the status. once complete, stop the timer.

the implementation details can vary. It would look different depending if you are using webforms or an MVC framework.

in this setup there is no threading, just a series of requests and responses.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I was able to do what I was after using threading.

all I had to do was Create a session variable with SyncLock and then update it within Callback Function.

while keep refreshing the page(via meta refresh) or ajax I was able to track the status of my thread.

cheeeeeers:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top