Hi
i found some difficulties in creating a class that would represent thread ( in need 10 instances) that works across two or more windows forms
i used this procedural approach to accomplish the task;
can any one help how to code it in a separate class
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ProvLib; using System.Threading; using System.Data.SqlClient; namespace WinFrmProv { public partial class TignDlg : Form { public TignDlg() { // formatDate(DateTime.Now); InitializeComponent(); InitListCtr(); addcolmns(); CDBProcessHandlerEx cdb = new CDBProcessHandlerEx(); string dbserv = cdb.GetDBServerNames(2000,1);// timer = new System.Threading.Timer(AllThreadFire, null, TimeSpan.Zero, new TimeSpan(0, 0, 1, 0)); } private void Form1_Load(object sender, EventArgs e) { // checkOrders(CDBProcessHandlerEx.g_threadSetup[1].DbNo, CDBProcessHandlerEx.g_threadSetup[1].LocaleNo, ref tignnj1Status); timer = new System.Threading.Timer(AllThreadFire, null, TimeSpan.Zero, new TimeSpan(0, 0, 0,2 )); } System.Threading.Timer timer; long intv0 = 0, intv1 = 0, intv2 = 0, intv3 = 0, intv4 = 0, intv5 = 0, intv6 = 0, intv7 = 0; bool first0 = true, first1 = true, first2 = true, first3 = true, first4 = true, first5 = true, first6 = true, first7 = true, first8 = true, first9 = true; public static bool wake0 = false, wake1 = false, wake2 = false, wake3 = false, wake4 = false, wake5 = false, wake6 = false, wake7 =false; public static bool wakeSleep0 = false, wakeSleep1 = false, wakeSleep2 = false, wakeSleep3 = false, wakeSleep4 = false, wakeSleep5 = false, wakeSleep6 = false, wakeSleep7 = false; int njlastMinute = 0; int nylastMinute = 0, tignnj1lastMinute = 0, tignnj3lastMinute =0, tignnj4lastMinute = 0, tignnj5lastMinute = 0, njdblalastMinute =0 , tignmastlastMinute = 0, tignmast2lastMinute = 0, castlelalastMinute = 0; //status public static int nylastStatus = 0, tignnj1Status = 0, tignnj3Status = 0, tignnj4Status =0, tignnj5Status = 0, njdblaStatus = 0 , tignmastStatus =0, tignmast2Status = 1, castlelaStatus = 1; int ERROR = 1, SLEEPING = 2, RUNNING = 3 ; string formatDate( DateTime dt) { // DateTime frmdate = DateTime .MaxValue; string frmstr = null; // string st=dt.ToString(); // string pmam = st[st.Length - 2].ToString() + st[st.Length - 1].ToString(); //StringBuilder bldr = new StringBuilder(st.Replace(pmam,"")); int yr = dt.Year; int mon=dt.Month ; int dy=dt.Day; int hr = dt.Hour; int min = dt.Minute; int sec = dt.Second; if (sec > 30) min++; if (min > 59) { min--; hr++; } //if (pmam == "AM") { //frmdate = new DateTime(yr, mon, dy, hr, min, sec); frmstr = mon.ToString() + "\\" + dy + "\\" + yr.ToString() + " " + hr + ":" + min; return frmstr; } /* else if (pmam == "PM") { //hr = hr + 12; frmstr = yr.ToString() + "/" + mon.ToString() + "/" + dy + " " + hr + ":" + min; return frmstr; }*/ return null; } int checkOrders(int dbNo,int localNo,bool firsttime, ref int threadStatus) { CDBProcessHandlerEx db = new CDBProcessHandlerEx(); DataSet ds= db.CheckForOrders(dbNo, localNo,ref threadStatus); foreach (DataRow dr in ds.Tables[0].Rows) { if (Convert.ToInt16(dr[0]) == 1) { threadStatus = ERROR; } if (Convert.ToInt16(dr[1]) == 0||firsttime) { // MessageBox.Show(""); threadStatus = SLEEPING; } else { threadStatus = RUNNING; } } // if (firsttime) threadStatus = SLEEPING; // MessageBox.Show(threadStatus.ToString()); return 0; } void AllThreadFire(object st) { var njminute = DateTime.Now.Minute; intv1 = CSchedule._Events[1].Interval / 60; if (njminute != njlastMinute && njminute % intv1 == 0 || first1||wake1 ||wakeSleep1) { checkOrders(CDBProcessHandlerEx.g_threadSetup[1].DbNo, CDBProcessHandlerEx.g_threadSetup[1].LocaleNo,first1, ref tignnj3Status); njlastMinute = njminute; this.tignNJ3Thread = new Thread(new ThreadStart(this.NjThreadProcSafe)); this.tignNJ3Thread.Name = "1"; this.tignNJ3Thread.Start(); first1 = false; // wake1 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[1].DbNo, CDBProcessHandlerEx.g_threadSetup[1].LocaleNo, ref tignnj1Status); } var nyminute = DateTime.Now.Minute; intv2 = CSchedule._Events[2].Interval / 60; if ((nyminute != nylastMinute && nyminute % intv2 == 0) || first2||wake2||wakeSleep2) { checkOrders(CDBProcessHandlerEx.g_threadSetup[2].DbNo, CDBProcessHandlerEx.g_threadSetup[2 ].LocaleNo,first2, ref tignnj4Status); nylastMinute = nyminute; this.tignNJ4Thread = new Thread(new ThreadStart(this.NyThreadProcSafe)); this.tignNJ4Thread.Name = "2"; this.tignNJ4Thread.Start(); first2 = false; //wake2 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[2].DbNo, CDBProcessHandlerEx.g_threadSetup[2].LocaleNo,first2, ref tignnj4Status); } var tignnj1minute = DateTime.Now.Minute; intv0 = CSchedule._Events[0].Interval/60; if (tignnj1minute != tignnj1lastMinute && tignnj1minute % intv0 == 0 || first0||wake0||wakeSleep0) { checkOrders(CDBProcessHandlerEx.g_threadSetup[0].DbNo, CDBProcessHandlerEx.g_threadSetup[0].LocaleNo,first0, ref tignnj1Status); tignnj1lastMinute = tignnj1minute; this.tignNJ1Thread = new Thread(new ThreadStart(this.Tignnj1ThreadProcSafe)); this.tignNJ1Thread.Name = "0"; this.tignNJ1Thread.Start(); first0 = false; // wake0 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[0].DbNo, CDBProcessHandlerEx.g_threadSetup[0].LocaleNo,first3, ref tignnj1Status); } var tignnj5minute = DateTime.Now.Minute; intv3 = CSchedule._Events[3].Interval / 60; if (tignnj5minute != tignnj5lastMinute && tignnj1minute % intv3 == 0 || first4||wake3 ||wakeSleep3 ) { checkOrders(CDBProcessHandlerEx.g_threadSetup[3].DbNo, CDBProcessHandlerEx.g_threadSetup[3].LocaleNo,first4, ref tignnj5Status); tignnj5lastMinute = tignnj5minute; this.tignNJ5Thread = new Thread(new ThreadStart(this.Tignnj5ThreadProcSafe)); this.tignNJ5Thread.Name = "3"; this.tignNJ5Thread.Start(); first4 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[3].DbNo, CDBProcessHandlerEx.g_threadSetup[3].LocaleNo,first4, ref tignnj5Status); } var njdblaminute = DateTime.Now.Minute; intv4 = CSchedule._Events[4].Interval / 60; if (njdblaminute != njdblalastMinute && njdblaminute % intv4 == 0 || first5||wake4 ||wakeSleep4) { checkOrders(CDBProcessHandlerEx.g_threadSetup[4].DbNo, CDBProcessHandlerEx.g_threadSetup[4].LocaleNo, first5, ref njdblaStatus); njdblalastMinute = njdblaminute; this.njdbLaThread = new Thread(new ThreadStart(this.NJdblaThreadProcSafe)); this.njdbLaThread.Name = "4"; this.njdbLaThread.Start(); first5 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[4].DbNo, CDBProcessHandlerEx.g_threadSetup[4].LocaleNo,first5, ref njdblaStatus); } var tignmastminute = DateTime.Now.Minute; intv5 = CSchedule._Events[5].Interval / 60; if (tignmastminute != tignmastlastMinute && tignmastminute % intv5 == 0 || first6 ||wake5 ||wakeSleep5) { checkOrders(CDBProcessHandlerEx.g_threadSetup[5].DbNo, CDBProcessHandlerEx.g_threadSetup[5].LocaleNo, first6, ref tignmastStatus); tignmastlastMinute = tignmastminute; this.tignMastThread = new Thread(new ThreadStart(this.TignmastThreadProcSafe)); this.tignMastThread.Name = "5"; first6 = false; this.tignMastThread.Start(); //checkOrders(CDBProcessHandlerEx.g_threadSetup[5].DbNo, CDBProcessHandlerEx.g_threadSetup[5].LocaleNo,first6, ref tignmastStatus); } var tignmast2minute = DateTime.Now.Minute; intv6 = CSchedule._Events[6].Interval / 60; if (tignmast2minute != tignmast2lastMinute && tignmast2minute % intv6 == 0 || first7 ||wake6 ||wakeSleep6) { checkOrders(CDBProcessHandlerEx.g_threadSetup[6].DbNo, CDBProcessHandlerEx.g_threadSetup[6].LocaleNo, first7, ref tignmast2Status); tignmast2lastMinute = tignmast2minute; this.tignMast2Thread = new Thread(new ThreadStart(this.Tignmast2ThreadProcSafe)); this.tignMast2Thread.Name = "6"; this.tignMast2Thread.Start(); first7 = false; //checkOrders(CDBProcessHandlerEx.g_threadSetup[6].DbNo, CDBProcessHandlerEx.g_threadSetup[6].LocaleNo, first7, ref tignmast2Status); } var castlelaminute = DateTime.Now.Minute; intv7 = CSchedule._Events[7].Interval / 60; if (tignmast2minute != castlelalastMinute && castlelaminute %intv7 == 0 || first8 ||wake7 ||wakeSleep7) { checkOrders(CDBProcessHandlerEx.g_threadSetup[7].DbNo, CDBProcessHandlerEx.g_threadSetup[7].LocaleNo, first8, ref castlelaStatus); castlelalastMinute = castlelaminute; this.castleLaThraed = new Thread(new ThreadStart(this.CastlelaThreadProcSafe)); this.castleLaThraed.Name = "7"; this.castleLaThraed.Start(); first8 = false; // checkOrders(CDBProcessHandlerEx.g_threadSetup[7].DbNo, CDBProcessHandlerEx.g_threadSetup[7].LocaleNo,first8, ref castlelaStatus); } } void LogonTodatabaseServer() { string constr = ("data source=Velocis initial catalog=acctdb;uid=provsvr;pwd=provsvr"); SqlCommand cmnd = null; SqlConnection conn = null; DataSet ds = new DataSet(); using (conn = new SqlConnection(constr)) { using (cmnd = new SqlCommand("", conn)) { cmnd.CommandText = "cmd2"; //cmnd.CommandType = CommandType.StoredProcedure; //cmnd.CommandText = "CheckForOrders"; //cmnd.Parameters.Add("@DBNo", SqlDbType.Int).Value = dbNo; //cmnd.Parameters.Add("@LocaleNo", SqlDbType.Int).Value = localeNo; //cmnd.Parameters.Add("@DEBUG", SqlDbType.Int).Value = 0;// localeNo; SqlDataAdapter da = new SqlDataAdapter(cmnd); try { conn.Open(); } catch (SqlException sx) { ViewThread.board = "\n unable to log on to server"; } } } } void addcolmns() { ListViewItem itm; // CDBProcessHandlerEx ex = new CDBProcessHandlerEx(); string[] tm = new string[8]; int ln = CDBProcessHandlerEx.SwitchObjects.Count; for (int i = 0; i < ln; i++) { tm = new string[8]; tm[0] = CDBProcessHandlerEx.SwitchObjects[i].dbserver; tm[1] = CDBProcessHandlerEx.SwitchObjects[i].localNo.ToString(); tm[2] = CDBProcessHandlerEx.SwitchObjects[i].dbNo.ToString(); tm[3] = CDBProcessHandlerEx.SwitchObjects[i].localeName; // tm[4] = "";// CDBProcessHandlerEx.SwitchObjects[i].localeName; // tm[5] = "";// CDBProcessHandlerEx.SwitchObjects[i].localeName; itm = new ListViewItem(tm); listView1.Items.Add(itm);//.Items.Add( listView1.Items[0].Name = (i).ToString(); // MessageBox.Show(listView1.Items[0].Name); } } #region // This event handler creates a background thread that // attempts to set a Windows Forms control property // directly. Thread tignNJ3Thread, tignNJ4Thread, tignNJ1Thread, tignNJ5Thread, njdbLaThread, tignMastThread, tignMast2Thread, castleLaThraed, radGadThread, radGad2Thread; delegate void tignnj3Thread(string status, int services, int id, string nextrun); delegate void tignnj4Thread(string status, int services, int id, string nextrun); delegate void tignnj1Thread(string status, int services, int id, string nextrun); delegate void njdblaThread(string status, int services, int id, string nextrun); delegate void tignnj5Thread(string status, int services, int id, string nextrun); delegate void tignmastThread(string status, int services, int id, string nextrun); delegate void tignmast2Thread(string status, int services, int id, string nextrun); delegate void castlelaThread(string status, int services, int id, string nextrun); delegate void radgadThread(string status, int services, int id, string nextrun); delegate void radgad2Thread(string status, int services, int id, string nextrun); #endregion // // It's on the same thread, no need for Invoke // // this.textBox1.Text = text + " (No Invoke)"; //} private void SetText( string status, int services, int id, string nextrun) { lock (listView1 ) { { listView1.Items[id].SubItems[4].Text = ""; //Thread.Sleep(300); listView1.Items[id].SubItems[4].Text = status; listView1.Items[id].SubItems[5].Text = services.ToString(); listView1.Items[id].SubItems[6].Text = id.ToString(); switch (nextrun) { case "0": if (!wake0) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[0].RunTime).Add(new TimeSpan(-8, (int)intv0, 0)));//.ToString(); } else { listView1.Items[id].SubItems[7].Text = ""; wake0 = false; } break; case "1": if (!wake1) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[1].RunTime).Add(new TimeSpan(-9, (int)intv1 - 18, 0)));//.ToString(); } else { listView1.Items[id].SubItems[7].Text = ""; wake1 = false; } break; case "2": if (!wake2) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[2].RunTime).Add(new TimeSpan(-9, (int)intv2 - 21, 0)));//.ToString(); } else { listView1.Items[id].SubItems[7].Text = ""; wake2 = false; } break; case "3": if (!wake3) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[3].RunTime).Add(new TimeSpan(-16, (int)intv3 - 1, 0)));//.ToString(); }else { listView1.Items[id].SubItems[7].Text = ""; wake3 = false; } break; case "4": if (!wake4) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[4].RunTime).Add(new TimeSpan(-16, (int)intv4 - 4, 0)));//.ToString(); }else { listView1.Items[id].SubItems[7].Text = ""; wake4 = false; } break; case "5": if (!wake5) { listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[5].RunTime).Add(new TimeSpan(-16, (int)intv5, 0)));///.ToString(); }else { listView1.Items[id].SubItems[7].Text = ""; wake5 = false; } break; case "6": if (!wake6) listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[6].RunTime).Add(new TimeSpan(-16, (int)intv6 - 1, 0)));//.ToString() ; else { listView1.Items[id].SubItems[7].Text = ""; wake6 = false; } break; case "7": if (!wake7) listView1.Items[id].SubItems[7].Text = formatDate(DateTime.Now.AddSeconds(CSchedule._Events[7].RunTime).Add(new TimeSpan(-15, (int)intv7 - 2, 0)));//.ToString(); else { listView1.Items[id].SubItems[7].Text = ""; wake7 = false; } break; case "radgad1": listView1.Items[id].SubItems[7].Text = DateTime.Now.AddMinutes(2.00).ToString(); break; case "radgad2": listView1.Items[id].SubItems[7].Text = DateTime.Now.AddMinutes(2.00).ToString(); break; } // listView1.Items[id].SubItems[7].Text = DateTime.Now.ToString ();//.AddMinutes(2.00).ToString(); } } } int id = 0; public int ID { get { return id; } set { id = value; } } private void NjThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignnj3Status == SLEEPING) status = "Sleeping"; if (tignnj3Status == RUNNING) { status = "Running"; LogonTodatabaseServer(); } if (tignnj3Status == ERROR) status = "Error"; tignnj3Thread d = new tignnj3Thread(SetText); if (wake3) { status = "Running"; ViewThread.board = "\n trying to log in"; wake3 = false; } if (wakeSleep3) { status = "Sleeping"; ViewThread.board = "\n Sleeping"; wakeSleep3 = false; } this.Invoke(d, new object[] { status, 0, 1, tignNJ3Thread.Name }); } else { // listView1.Items[id].SubItems[7].Text = DateTime.Now.AddMinutes(4.00).ToString(); } } string status0 = "Sleeping"; private void Tignnj1ThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignnj1Status == SLEEPING) status = "Sleeping"; if (tignnj1Status == RUNNING) status = "Running"; if (tignnj1Status == ERROR) status = "Error"; if (wake0) { status = "Running"; ViewThread.board ="\n trying to log in"; wake0 = false; } if (wakeSleep0) { status = "Sleeping"; ViewThread.board = "\n Sleeping"; wakeSleep0 = false; } tignnj1Thread d = new tignnj1Thread(SetText); this.Invoke(d, new object[] { status , 0, 0, tignNJ1Thread.Name }); } } void callUpdate(string s) { ViewThread.board += " trying to log in"; } private void Tignnj5ThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignnj5Status == SLEEPING) status = "Sleeping"; if (tignnj5Status == RUNNING) status = "Running"; if (tignnj5Status == ERROR) status = "Error"; if (wake3) { status = "Running"; ViewThread.board = "\n trying to log in"; wake3 = false; } tignnj5Thread d = new tignnj5Thread(SetText); this.Invoke(d, new object[] { status , 0, 3, tignNJ5Thread.Name }); } else { //lock(listView1) //listView1.Items[3].SubItems[7].Text = DateTime.Now.AddMinutes(4.00).ToString(); } } private void NJdblaThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (njdblaStatus == SLEEPING) status = "Sleeping"; if (njdblaStatus == RUNNING) status = "Running"; if (njdblaStatus == ERROR) status = "Error"; if (wake4) { status = "Running"; ViewThread.board = "\n trying to log in"; wake4 = false; } njdblaThread d = new njdblaThread(SetText); this.Invoke(d, new object[] { status , 0, 4, njdbLaThread.Name }); } } private void TignmastThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignmastStatus == SLEEPING) status = "Sleeping"; if (tignmastStatus == RUNNING) status = "Running"; if (tignmastStatus == ERROR) status = "Error"; if (wake5) { status = "Running"; ViewThread.board = "\n trying to log in"; wake5 = false; } tignmastThread d = new tignmastThread(SetText); this.Invoke(d, new object[] {status , 0, 5, tignMastThread.Name }); } } private void Tignmast2ThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignmast2Status == SLEEPING) status = "Sleeping"; if (tignmast2Status == RUNNING) status = "Running"; if (tignmast2Status == ERROR) status = "Error"; if (wake6) { status = "Running"; ViewThread.board = "\n trying to log in"; wake6 = false; } tignmast2Thread d = new tignmast2Thread(SetText); this.Invoke(d, new object[] {status , 0, 6, tignMast2Thread.Name }); } } private void CastlelaThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (castlelaStatus == SLEEPING) status = "Sleeping"; if (castlelaStatus == RUNNING) status = "Running"; if (castlelaStatus == ERROR) status = "Error"; if (wake7) { status = "Running"; ViewThread.board += "\n trying to log in"; wake7 = false; } castlelaThread d = new castlelaThread(SetText); this.Invoke(d, new object[] { status , 0, 7, castleLaThraed.Name }); } } private void NyThreadProcSafe() { if (this.listView1.InvokeRequired) { string status = ""; if (tignnj4Status == SLEEPING) status = "Sleeping"; if (tignnj4Status == RUNNING) status = "Running"; if (tignnj4Status == ERROR) status = "Error"; if (wake4) { status = "Running"; ViewThread.board += "\n trying to log in"; wake4 = false; } tignnj4Thread d1 = new tignnj4Thread(SetText); this.Invoke(d1, new object[] {status , 0, 2, tignNJ4Thread.Name }); } } // This method is executed on the worker thread. It attempts // to access the TextBox control directly, which is not safe. void InitListCtr() { listView1.Columns.Add("DB Server Name", 80, HorizontalAlignment.Left); listView1.Columns.Add("Locale No ", 80, HorizontalAlignment.Left); listView1.Columns.Add("Db No", 65, HorizontalAlignment.Left); listView1.Columns.Add("Locale ", 85, HorizontalAlignment.Left); listView1.Columns.Add("Status ", 65, HorizontalAlignment.Left); listView1.Columns.Add("Service ", 65, HorizontalAlignment.Left); listView1.Columns.Add("Records", 65, HorizontalAlignment.Left); listView1.Columns.Add("Next Run ", 135, HorizontalAlignment.Left); } private void btnQuit_Click(object sender, EventArgs e) { this.Hide(); } private void button1_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { MessageBox.Show("must"); return; } ViewThread vw = new ViewThread(); vw.Text = listView1.SelectedItems[0].Text ; ViewThread.board = "posting "; vw.ShowDialog(this); } private void btnSetup_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { MessageBox.Show("must select service"); return; } TignThreadSetup tg = new TignThreadSetup(); int id=listView1.SelectedItems[0].Index; tg.Text= CDBProcessHandlerEx.g_threadSetup[id].Title.Replace ("'+char(9)+'"," "); tg.ShowDialog(this); } public static string NextRunTime { get; set; } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { // if (listView1.SelectedItems.Count == 0) // //string nm= listView1.SelectedItems[0].Text; } } }