Passing data from datareceived to another thread

  • 13 years ago
    Hello all,

    I am writting an application for windows CE 5.0 and i am trying to make it multithreaded.
    The problem I am having is how to pass data from one thread to another...

    This is what I have so far...
    In a thread on main form I send a string of request to a Send function that is in module functions, then i want this thread to wait till the datareceived event that is in module functions fires up receives an answer on serial port and sends received data to thread which can then continue with its work...
    Can someone please tell me how to do this if it is possible?.

    Private Sub MeasureFlow()
            functions.send("SENDFLOW")
            'now this thread should wait for a reply from datareceived event....
            'when reply comes thread can continue...
    End Sub


    Thanks in advance
    Bye






















  • 13 years ago

    i usually do this through an implementation of the WaitQueue ADT,

    i'm pasting a sample c# code, you need to convert it to VB.NET

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Threading;



    namespace System.Collections.Extra
    {
        class WaitQueue<CType> : IDisposable
        {
            Queue<CType> data = new Queue<CType>();
            Semaphore sem = new Semaphore(0,1);
            Object __datadeq = new Object();
            Object __dataenq = new Object();






            public CType Dequeue()
            {
                lock (__datadeq)
                {
                    if(this.data.Count == 0)
                        sem.WaitOne();




                    return data.Dequeue();
                }
            }

            public void Enqueue(CType data)
            {
                lock (__dataenq)
                {
                    this.data.Enqueue(data);
                    if(this.data.Count==1)
                        sem.Release();
                }
            }







            public void Dispose()
            {
                sem.Close();
                this.data.Clear();
            }



        }
    }


  • 13 years ago

    Use anonymous delegates to pass the data to new thread. Here is the code

    public

    class Logger : IDisposable

    {

    public

    void Error(string message)
    {

    /// Create thread with anonymous delegate and pass LogEntry object to it
    Thread
    t = new Thread(delegate() { this.LogMessage(new LogEntry("EROR", message)); });
    t.Start();

    }

    ///

    Thread entry point
    private
    void LogMessage(LogEntry dequed)
    {

    string logEntry = DateTime.Now.ToShortDateString() + ", " + dequed.Type + ", "+ dequed.Message;

    }

    }

    Hope this helps.

    -Sanjay Gadge

  • 13 years ago
    sanjay.gadge wrote:

    Use anonymous delegates to pass the data to new thread. Here is the code

    public

    class Logger : IDisposable

    {

    public

    void Error(string message)
    {

    /// Create thread with anonymous delegate and pass LogEntry object to it
    Thread
    t = new Thread(delegate() { this.LogMessage(new LogEntry("EROR", message)); });
    t.Start();

    }

    ///

    Thread entry point
    private
    void LogMessage(LogEntry dequed)
    {

    string logEntry = DateTime.Now.ToShortDateString() + ", " + dequed.Type + ", "+ dequed.Message;

    }

    }

    Hope this helps.

    -Sanjay Gadge

    Unfortunatly VB.net doesn't have anonymous delegates. 
  • 13 years ago

    Sorry I do not know anything about VB/VB.Net.

    But have a look at http://www.yoda.arachsys.com/csharp/threadstart.html. One of the technique here might be useful.

    Cheers
    Sanjay

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson