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:18 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 Can any one help to solve the Thread error here



I am getting the following error when i execute the program.

Server Error in '/threaad' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 48: for(int i=1;i<20;i++)
Line 49: {
Line 50: t[i].Start();
Line 51: }
Line 52:


Source File: c:\inetpub\wwwroot\threaad\webform1.aspx.cs Line: 50

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   threaad.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\threaad\webform1.aspx.cs:50
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()



<CODE Here>

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];
            for(int i=1;i<20;i++)
            {
                t[i].Start();
            }


            DateTime start = DateTime.Now;
            Response.Write("PROGRAM STARTED.AT.."+start.TimeOfDay );
            Response.Write("<br>");

            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();

        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

Vivek Shah
__________________
Vivek Shah
 
Old April 12th, 2006, 05:10 AM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You haven't populated your array of Threads t, with your arrays.
You need to do:
            Thread [] t = new Thread[20];
t[0] = t1;
..
t[19] = t20;

but that's really tedious, why are you holding the temporaries?
You could just use:

            Thread [] t = new Thread[20];
            for(int i=1;i<20;i++)
            {
                t[i] = new Thread( new ThreadStart( sayHello));
                t[i].Start();
            }

Also, don't you want to write out your start time before kicking off the threads?

And the end time as currently displayed will just be the time taken to start the threads, I'm presuming that you want to wait until all of the threads have actually finished? The easily way to do this is to Join on all of the threads.






Similar Threads
Thread Thread Starter Forum Replies Last Post
cross thread Error angelboy C# 2005 1 July 28th, 2007 04:35 AM
How to solve this error? ramk_1978 Classic ASP Components 1 June 27th, 2006 02:51 AM
Can anyone help me to solve this error vikaspa VB How-To 1 October 5th, 2005 04:43 AM
Error that I could not solve sajjad27 VS.NET 2002/2003 0 August 17th, 2003 11:40 PM





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