CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2006
    Posts
    42

    Read/Write from another Thread

    I have to implement a trace window in my application. So I have a List (System.Collections.List<>) which serves as a buffer and a grid control which serves as trace output. As items are being added to the buffer list, I want to update my grid with the newly arrived data. So I thought of creating a thread that polls the list. This is the method to add elements:

    Code:
            List<TraceRow> _detectedResults;
            public void AddToList(TraceRow row)
            {
                lock (_detectedResults)
                {
                    _detectedResults.Add(row);
                }
            }
    The thread:

    Code:
                Thread readFromListThread = new Thread(ReadDataFromList);
                readFromListThread.Start();
    and the polling:

    Code:
    static void ReadDataFromList()
    {
            while(true)
            {
                      lock(_detectedResults)
                      {
                            foreach (TraceRow traceRow in _detectedResults)
                            {
                                  FillGridControl(traceRow);  
                            }
                       }
             }
    }
    The problem is, this is very slow and eats too much of the resources. Also I have a feeling that there is another way to do this...any suggestions?

  2. #2
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: Read/Write from another Thread

    Use events.

    Well, you can create a collection class which contains all entries like you described. Then you put an changed event into the collection. Every time an entry is added to the list the changed event will be thrown.
    Register an eventhandler on your GUI to the collection and refresh the GUI if the event is thrown. Should be easy to implement and you need no stupid blocking threads.
    Useful or not? Rate my posting. Thanks.

  3. #3
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Read/Write from another Thread

    Do what torrud said. I just want to underscore the idea that "polling" is bad, and "pushing" is good. To be clear, ill give an example of polling vs pushing.

    Alpha wants to know if Beta has some new data:

    Polling: Non-Event driven design (bad)

    Alpha calls Beta every N seconds to see if Beta has new data. Alpha does a lot of wasted work because Beta frequently does not have new data.

    Pushing: Event Driven design (good)

    Alpha subscribes to a NewData event on Beta.
    Beta does its thing and whenver new data appears, it invokes the event handler NewData. This does not do any unecessairy work

  4. #4
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Read/Write from another Thread

    Quote Originally Posted by torrud
    Use events.

    Well, you can create a collection class which contains all entries like you described. Then you put an changed event into the collection. Every time an entry is added to the list the changed event will be thrown.
    Register an eventhandler on your GUI to the collection and refresh the GUI if the event is thrown. Should be easy to implement and you need no stupid blocking threads.
    The key to this is delegation. This suggestion is useless without it because the event will be raised on the worker thread so accessing any UI elements, i.e. controls, from the event handler will throw an exception. Use a BackgroundWorker if it's appropriate or else implement your own delegation.

    http://www.vbforums.com/showthread.php?t=471889
    http://www.vbforums.com/showthread.php?t=498387
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Read/Write from another Thread

    It wonn't throw an exception if you do it right. How else do you think people do asynchronous work other than using separate threads?

    In your event handler you just have to check if you need to switch to the main thread to modify the control. Read here for more info: http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Nov 1999
    Location
    Denmark
    Posts
    260

    Re: Read/Write from another Thread

    and use "InvokeRequired" is a good idea if other threads access guiElements in another

  7. #7
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Read/Write from another Thread

    Am I being punk'd here? Using the InvokeRequired property and the Invoke method IS using delegation, which is exactly what I said needed to be done. The InvokeRequired property tells you whether delegation, i.e. marshalling to the control's owning thread, is needed. If it is you create a delegate and pass that to the Invoke method, which marshals the call to the owning thread and invokes the delegate, which then invokes the method it refers to. If you follow the second link in post #4 you'll find my detailed explanation of how and why to do it.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: Read/Write from another Thread

    I think i just misunderstood what you were saying. I thought you were saying that if you used a special thread to process the data, it would result in an exception being thrown. My bad!
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

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

Featured