none
Could I pass data between threads?

    Question

  • I create a DataTable in one thread, when I pass it to another thread, the data in it seems corrupted. How can I pass data between threads?

    Thanks!
    Tuesday, August 23, 2005 12:26 AM

Answers

  • Hi,

    How are you trying to pass data between threads? A good way would be to have the DataTable as a class level variable and then access with appropriate synchronization techniques such as locks.

    Regards,
    Vikram
    Tuesday, August 23, 2005 3:48 AM
  • Hi,

    Where is the data declared ? How are you passing it?
    Did you try the approach I suggested of having the DataTable as a class level variable. In that case, its not even passing the data, its just that the data is in the scope of the both threads and both can access it.

    Regards,
    Vikram
    Tuesday, August 23, 2005 5:20 AM

All replies

  • Hi,

    How are you trying to pass data between threads? A good way would be to have the DataTable as a class level variable and then access with appropriate synchronization techniques such as locks.

    Regards,
    Vikram
    Tuesday, August 23, 2005 3:48 AM
  • I just create a DataTable in the background thread, and pass it out to the main thread. Since the access to the DataTable is by sequence, I don't think it's necessary to take a lock.
    Tuesday, August 23, 2005 4:08 AM
  • Even in that case you could still use the same approach which I mentioned without locks.

    Regards,
    Vikram

    Tuesday, August 23, 2005 4:19 AM
  • Yes, but the problem is not of the lock, the problem is that the data could not be passed through.
    Tuesday, August 23, 2005 4:44 AM
  • Hi,

    Where is the data declared ? How are you passing it?
    Did you try the approach I suggested of having the DataTable as a class level variable. In that case, its not even passing the data, its just that the data is in the scope of the both threads and both can access it.

    Regards,
    Vikram
    Tuesday, August 23, 2005 5:20 AM
  • Hi,

    If you dont want to declare a class level DataSet variable,you can just pass the Dataset object to a method in main thread.



    using System;
    using System.IO;
    using System.Threading;

    namespace ThreadMethod
    {

    class Class1
    {
    Class1()
    {
    Thread tThread = new Thread(new ThreadStart(TMethod));
    tThread.Start();
    }

    void CallBack(string msg)
    {
    Console.WriteLine(msg);
    Console.ReadLine();
    }

    void TMethod()
    {
    string msg = "Hi..This is Suresh here";
    CallBack(msg);
    }

    [STAThread]
    static void Main(string[] args)
    {
    Class1 obj = new Class1();
    }
    }
    }

     


    Try replacing the string with DataSet..

    Suresh.
    Saturday, August 27, 2005 6:51 AM