线程数共享此线程在另一个线程内启动的一个函数实例

时间:2011-05-30 09:02:54

标签: c# .net multithreading

当我们创建新的实例线程&传递函数

var threaddatatable = new System.Threading.Thread(enqueue);
threaddatatable.Start(dt);
然后它将创建函数的新实例是对的。 那么为什么线程共享变量数据。

public void enqueue(object e)
{
    try
    {            
        DataChangeEventArgs e113 = (DataChangeEventArgs)e;
        resetdatasource();
        for (i=0; i < e113.sts.Length; i++)
        {                           
            int hour113 = e113.sts[i].TimeStampNet.Hour;
            int minute113 = e113.sts[i].TimeStampNet.Minute;
            int second113 = e113.sts[i].TimeStampNet.Second;
            int millisecond113 = e113.sts[i].TimeStampNet.Millisecond;
            int year113 = e113.sts[i].TimeStampNet.Year;
            int month113 = e113.sts[i].TimeStampNet.Month;
            int day113 = e113.sts[i].TimeStampNet.Day;
            DateTime sdate113 = new DateTime(year113, month113, day113, hour113, minute113, second113, millisecond113);

            DataRow row1 = dt.NewRow();
            row1["itemID"] = e113.sts[i].HandleClient;
            row1["paramvalue"] = convert.ToString(e113.sts[i].DataValue);
            row1["date_logged1"] = sdate113.ToString("dd-MM-yyyy HH:mm:ss.fff");
            row1["Quality"] = e113.sts[i].Quality;
            row1["date_logged"] = DateTime.FromFileTime(e113.sts[i].TimeStamp);
            dt.Rows.Add(row1);
        }
        var threadupdate = new Thread(update);
        threadupdate.Start(dt);
    }
    catch { }
}
public void resetdatasource()
{
    dt = new DataTable();
    DataColumn col1 = new DataColumn("itemID", typeof(int));
    dt.Columns.Add(col1);
    DataColumn col2 = new DataColumn("paramvalue", typeof(string));
    dt.Columns.Add(col2);
    DataColumn col3 = new DataColumn("date_logged1", typeof(string));
    dt.Columns.Add(col3);
    DataColumn col4 = new DataColumn("Quality", typeof(string));
    dt.Columns.Add(col4);
    DataColumn col5 = new DataColumn("date_logged", typeof(DateTime));
    dt.Columns.Add(col5);            
    dt.TableName = "parameter";
}

我将写这个然后它将抛出一个已经属于该表的异常行 它将被共享数据表 提前谢谢。

0 个答案:

没有答案