Results 1 to 8 of 8

Thread: Task Await/Threading

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Task Await/Threading

    Web Forms or MVC?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Task Await/Threading

    asp.Net Webforms

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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…

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width