p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   How do use Array with Threads (http://p2p.wrox.com/showthread.php?t=41081)

vivekshah April 11th, 2006 01:59 AM

How do use Array with Threads
 


How can we use array of threads in c#.

Can we also create threads dynamically for an application having 20 threads...?

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Data.SqlClient;
namespace threaad
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button btn1;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here

            Thread t1 = new Thread( new ThreadStart( sayHello));
            Thread t2 = new Thread( new ThreadStart( sayHello));
            Thread t3 = new Thread( new ThreadStart( sayHello));
            Thread t4 = new Thread( new ThreadStart( sayHello));
            Thread t5 = new Thread( new ThreadStart( sayHello));
            Thread t6 = new Thread( new ThreadStart( sayHello));
            Thread t7 = new Thread( new ThreadStart( sayHello));
            Thread t8 = new Thread( new ThreadStart( sayHello));
            Thread t9 = new Thread( new ThreadStart( sayHello));
            Thread t10 = new Thread( new ThreadStart( sayHello));
            Thread t11 = new Thread( new ThreadStart( sayHello));
            Thread t12 = new Thread( new ThreadStart( sayHello));
            Thread t13 = new Thread( new ThreadStart( sayHello));
            Thread t14 = new Thread( new ThreadStart( sayHello));
            Thread t15 = new Thread( new ThreadStart( sayHello));
            Thread t16 = new Thread( new ThreadStart( sayHello));
            Thread t17 = new Thread( new ThreadStart( sayHello));
            Thread t18 = new Thread( new ThreadStart( sayHello));
            Thread t19 = new Thread( new ThreadStart( sayHello));
            Thread t20 = new Thread( new ThreadStart( sayHello));

// Thread []t = new Thread[20];
//
// Response.Write(t.ThreadState.ToString());
            for(int i=0;i<20;i++)
            {

               t[i].Start();
            }


            DateTime start = DateTime.Now;
            Response.Write("PROGRAM STARTED.AT.."+start.TimeOfDay );
            Response.Write("<br>");
//
// for( int i=1; i<=20; i++)
// {
// t(i).Start();
// }
// t1.Start();
// t2.Start();
// t3.Start();
// t4.Start();
// t5.Start();
// t6.Start();
// t7.Start();
// t8.Start();
// t9.Start();
// t10.Start();
// t11.Start();
// t12.Start();
// t13.Start();
// t14.Start();
// t15.Start();
// t16.Start();
// t17.Start();
// t18.Start();
// t19.Start();
// t20.Start();

            DateTime end = DateTime.Now;
            Response.Write("\nPROGRAM ENDED...at "+end.TimeOfDay);
            Response.Write("<br><br>");
            Response.Write("\nTime Taken : " + (end.Millisecond - start.Millisecond));


            btn1.Attributes.Add("onclick","javascript:clicked( )");
        }
        static void sayHello()
        {
            SqlConnection con = new SqlConnection("server=comp17;database=POS;user id=sa;password=cmie");
            //string qry = "SELECT * FROM PRODUCTS WHERE MFG_NAME LIKE ' "+ condition + "%' ";
            string qry = "SELECT TOP 10000 * FROM PRODUCTS ";
            SqlCommand myCmd = new SqlCommand(qry,con);
            DataSet ds = new DataSet();
            myCmd.Connection.Open();
            myCmd.ExecuteNonQuery();

            myCmd.Connection.Close();

        }












Vivek Shah

nigelgandy June 26th, 2006 11:24 AM

Vivek,

Did you ever get any kind of answer to this? I'm trying to dynamically create a thread array in c# as well. Basically I am calling some data from a table. Each row in this table has some values I need. For each row, I'll need a thread spawned which calls a method that handles that data. (Each thread communicates with a 3rd party web service, so that's why I'm threading the method call)

something like so:
private int _methodCount;
public int MethodCount
{
    get
    {
        return SafeConvert.ToInt32(_methodCount);
    }
    set
    {
        _methodCount = value;
    }
}
private Thread [] arrThread
{
  get
    {
        return new Thread[MethodCount];
    }
}

private Thread providerThread
{
    get
    {
        return new Thread(new ThreadStart(GetResults)); // GetResults is the method that interacts with 3rd party web services
    }
}

void Method()
{
    ds = a dataset
    MethodCount = ds.Tables[0].Rows.Count;
    int x = 0;
    // Loop through the row count and spawn a thread for each
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        // ... do some code here, then start the thread for this row's data
        providerThread.Start();
        arrThread[x] = providerThread;
        x++;
    }

    // Now make sure all therads are completed, if not, don't continue with final code
    for (int i = 0; i < MethodCount; i++)
    {
        if (arrThread[i].ThreadState == ThreadState.Running)
        Thread.Sleep(1);
    }

    // ... final code here to run after all threads are completed.
}

Any thoughts???


All times are GMT -4. The time now is 11:26 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.