Results 1 to 2 of 2

Thread: Make a timer async and thread safe

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Make a timer async and thread safe

    Hi everybody, I want to make an async timer but I'm straggling a bit with thread safe.. I m not able not to call object from main thread. The code is this one:
    Code:
                                                Dim res As String
                                                Dim client As New WebClient()
    
                                                client.Encoding = Encoding.UTF8
                                                client.Headers.Add(HttpRequestHeader.UserAgent, "")
                                                Dim stream = client.OpenRead("url link")
                                                Dim sr As StreamReader = New StreamReader(stream)
                                                BetterListBox1.Items.Clear()
                                                res = (sr.ReadToEnd)
    
    
                                                'FILTRO I TAG HTML DAL CONTENUTO RAW
                                                ' Dim sourcestring As String = res
    
                                                Dim matchpattern As String = "<(?:[^>=]|='[^']*'|=""[^""]*""|=[^'""][^\s>]*)*>"
                                                Dim replacementstring As String = ""
                                                res = ((Regex.Replace(res, matchpattern, "")))
                                                res = WebUtility.HtmlDecode(res)
                                            Dim visibleItems As Integer = CInt(BetterListBox1.ClientSize.Height / BetterListBox1.ItemHeight)
    
                                            BetterListBox1.Items.AddRange(Split(res, vbLf))
                                            BetterListBox1.TopIndex = Math.Max(BetterListBox1.Items.Count - visibleItems + 1, 0)
    
                                            End Sub
    There is a way where I can make this code run async? Thanks

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

    Re: Make a timer async and thread safe

    What Timer? I commented on this question on SO and the it's just as inadequate a description here as it is there.

    Also, what would make you think that posting code with all that leading whitespace was a good idea? Please make an effort to help us help you by posting as clearly as possible. You can hold down the Alt key while selecting an arbitrary block of text in VS so there's no reason to ever post code with pointless whitespace.

    As for the threading issue, follow the CodeBank link in my signature below and check out my thread on Accessing Controls From Worker Threads.
    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

Tags for this Thread

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