Hello,

i have created Calender for a year and i m checking the existance of each day in database and filling color accodingly.Also in my calender i can make "sunday or sturday" to normal day.My form if gettting hang.

So i have used thread to load the calender but still i m not able to run my form .

Please help me , if anyone knows the solution.
Below is my code.

private void Calendar_Load(object sender, EventArgs e)
        {
  this.Cursor = Cursors.WaitCursor;
           thread = new Thread(new ThreadStart(LoadCurrentCalender));
            thread.Start();
}
  private void LoadCurrentCalender()
        {
            
            int CurrentYear = Convert.ToInt32(nud_year.Value);
            dgv_calendar.Rows.Clear();
            for (int i = 1; i <= 12; i++)
            {          
                DateTime tempdate = new DateTime(CurrentYear, i,15);
                int daysinmonth = DateTime.DaysInMonth(CurrentYear, i);
                int startcoulmnindex = 0,endcoulmnindex=0;
                #region SET START AND END COULMNINDEX
                switch (tempdate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        startcoulmnindex = 0; endcoulmnindex = daysinmonth;
                        break;
                    case DayOfWeek.Tuesday:
                        startcoulmnindex = 1; endcoulmnindex = daysinmonth + 1;
                        break;
                    case DayOfWeek.Wednesday:
                        startcoulmnindex = 2; endcoulmnindex = daysinmonth + 2;
                        break;
                    case DayOfWeek.Thursday:
                        startcoulmnindex = 3; endcoulmnindex = daysinmonth + 3;
                        break;
                    case DayOfWeek.Friday:
                        startcoulmnindex = 4; endcoulmnindex = daysinmonth +4;
                        break;
                    case DayOfWeek.Saturday:
                        startcoulmnindex = 5; endcoulmnindex = daysinmonth +5;
                        break;
                    case DayOfWeek.Sunday:
                        startcoulmnindex = 6; endcoulmnindex = daysinmonth + 6;
                        break;
                }
                #endregion
                int currentrowindex=i-1;
                DataGridViewRow dgrow = new DataGridViewRow();
                if (dgv_calendar.InvokeRequired)
                {
                    dgv_calendar.Invoke(new StatusPanelDelegate(LoadCurrentCalender));
                }
                dgv_calendar.Rows.Insert(currentrowindex, dgrow);
                dgv_calendar.Rows[currentrowindex].HeaderCell.Value = tempdate.ToString("MMMM");
                int temp = 0;
                for (int j = startcoulmnindex; j < endcoulmnindex; j++)
                {
                    #region DATEVALUE
                    temp++;
                    DateTime datevalu=new DateTime(CurrentYear, i,temp);
                    dgv_calendar.Rows[currentrowindex].Cells[j].Value = datevalu.Date;
                    #endregion
                    #region CHECK FOR EXISTINGCALENDER DATE
                    int dayid = 0;
                    BL_Calendar db = new BL_Calendar(Connstring);
                    db.changeddate = datevalu;
                    SqlDataReader reader = db.CheckExistingCalenderDateandGetValue();
                    if (reader != null)
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                dayid = Convert.ToInt32(reader["dayid"]);
                            }
                        }
                    }
                    #endregion
                    #region FOR NORMAL DAYS
                    if (dgv_calendar.Columns[j].HeaderText == "M" || dgv_calendar.Columns[j].HeaderText == "T" || dgv_calendar.Columns[j].HeaderText == "W" || dgv_calendar.Columns[j].HeaderText == "T" || dgv_calendar.Columns[j].HeaderText == "F")
                    {                     
                        if (dayid == 0)
                        {
                            dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.LightGreen;
                            dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = Color.LightGreen;
                        }
                        else
                        {
                            db.dayid = dayid;
                            db.action = "filter";
                            SqlDataReader dr = db.GetDayColor();
                            if (dr != null)
                            {
                                if (dr.Read())
                                {
                                    dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.FromName(Convert.ToString(dr["colorcode"]));
                                    dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor;
                                }
                            }
                        }
                    }
                    #endregion
                    #region FOR SATURDAY AND SUNDAy
                    else if (dgv_calendar.Columns[j].HeaderText == "S")
                    {
                        if (dgv_calendar.Columns[j].Name == "SUN1" || dgv_calendar.Columns[j].Name == "SUN2" || dgv_calendar.Columns[j].Name == "SUN3" || dgv_calendar.Columns[j].Name == "SUN4" || dgv_calendar.Columns[j].Name == "SUN5")
                        {
                            if (dayid == 0)
                            {
                                dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.Pink;
                                dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = Color.Pink;
                            }
                            else
                            {
                                db.dayid = dayid;
                                db.action = "filter";
                                SqlDataReader dr = db.GetDayColor();
                                if (dr != null)
                                {
                                    if (dr.Read())
                                    {
                                        dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.FromName(Convert.ToString(dr["colorcode"]));
                                        dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (dayid == 0)
                            {
                                dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.LightBlue;
                                dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = Color.LightBlue;
                            }
                            else
                            {
                                db.dayid = dayid;
                                db.action = "filter";
                                SqlDataReader dr = db.GetDayColor();
                                if (dr != null)
                                {
                                    if (dr.Read())
                                    {
                                        dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor = Color.FromName(Convert.ToString(dr["colorcode"]));
                                        dgv_calendar.Rows[currentrowindex].Cells[j].Style.ForeColor = dgv_calendar.Rows[currentrowindex].Cells[j].Style.BackColor;
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
            dgv_calendar.Rows[0].Cells[0].Selected = false;
            thread.Abort();
        }

Where exactly is it hanging? Have you run the code in the debugger and looked at the values of the variables? Have you single stepped through the code to see what was happening?

Hello,
Yes i have debug the code.Code is not throwing exception.
if i Debug the code step by step.it is running properly.
But when i run the form without debugger .the form gets hang.
Thanks

This article has been dead for over six months. Start a new discussion instead.