-
Task Await/Threading
I have some code that i run in a background thread in a winforms app, but i need to convert it for an Asp.Net app...
Code:
tmr.Start() ' Timers don't work in Asp.Net
thread = New Threading.Thread(AddressOf DoWork, 20971520)
thread.IsBackground = True
thread.Start()
Code:
Private Sub DoWork()
Dim game As New Game
Application.DoEvents() ' Application DoEvents doesn't work in Asp.Net
n = game.createNew2()
End Sub
Code:
Private Sub tmr_tick(sender As Object, e As EventArgs) Handles tmr.Tick
If Not thread.IsAlive Then ' this doesn't work properly
tmr.Stop()
' update ui here
' etc
End If
End Sub
Basically, I need to run the background thread with an increased stack size, then when it's completed, I need to update the ui.
This is a VS2019 app, so any improvements using async tasks and await would be appreciated...
-
Re: Task Await/Threading
-
Re: Task Await/Threading
-
Re: Task Await/Threading
I would think that you'd need to use a JavaScript timer in the page and go from there, but I haven't done any Web Forms for a long time so I'm not sure if there's a better option.
-
Re: Task Await/Threading
The timer isn’t an absolute necessity. There’s probably a workaround. I tried an asp:timer, but it just keeps ticking. Thread.isalive doesn’t seem to work, but I know the methods called in dowork are working correctly. It’s just a case of getting the thread finished, then getting the global information back to the Ui to interact with the html elements, at the right time…
-
Re: Task Await/Threading
It's been years since I did web development but on think for sure. Don't use timers on web applications, it just defeat the stateless purpose.
What I would have done is use either Ajax sync async calls or a callback (on client side javascript) and get a notification when server finishes.
For example: https://stackify.com/return-ajax-res...vascript-call/
Be advised that since it's asp.net it might get tricky. 8 years ago I could flip an error quickly but , again, it's been years...
Also new development tend to use Anqular but I think it only applies to MVC but it does not harm to post a link:
https://stackoverflow.com/questions/...-asp-net-web-a
-
Re: Task Await/Threading
Timers and DoEvents won't work in ASP.Net because they depend on message loops to function which Web applications do not have.
If you want to perform a background task and wait till it's finished, Tasks and Async/Await can make this easy:-
Code:
Imports System.Threading
Public Class Form1
Private Sub DoWork()
'Lets pretend this is a long running task.
For i = 1 To 10
Me.Invoke(Sub() Button1.Text = i.ToString())
Thread.Sleep(500)
Next
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim prevText As String = Button1.Text
Button1.Enabled = False
'Runs the work on a worker thread through a Task
'object which could be awaited
Await Task.Run(AddressOf DoWork)
MessageBox.Show("Task finished.")
Button1.Enabled = True
Button1.Text = prevText
End Sub
End Class
The above is an shows how Async, Await and Tasks are related to each other and how they could be used to perform a background task. My experience with ASP.Net is limited so far so I'm not sure exactly how this would fit into for your specific needs it but I do know that Tasks and Async/Await are used in ASP.Net.
-
Re: Task Await/Threading
What I haven't tried is NOT do and async on the client side and do one on the server or the opposite, do not async on the server async on the client.
Would be interesting but do not that since this is client to server talk there is no rule that the client must know what the server is doing with the data.
So it's one thing to use the code Niya posted to do an async on the server (I don't believe you will see a different on the page tho when you "pause" the server as pages are stateless) and another thing to get the async on client side.