Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2006, 01:59 AM
Authorized User
 
Join Date: Apr 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vivekshah Send a message via Yahoo to vivekshah
Default 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
__________________
Vivek Shah
 
Old June 26th, 2006, 11:24 AM
Registered User
 
Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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???





Similar Threads
Thread Thread Starter Forum Replies Last Post
generate too may threads mikel79 All Other Wrox Books 0 April 13th, 2006 07:52 AM
Struts and Threads varun_java Apache Tomcat 0 April 2nd, 2005 11:01 AM
My week with threads freezotic BOOK: Beginning Java 2 0 April 16th, 2004 04:27 PM
Threads Help hybrid_dev C# 0 July 31st, 2003 10:23 AM
Problems with threads. Threads disappearing? DriesNeyrinck VB.NET 0 June 10th, 2003 08:26 AM





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