Click here to Skip to main content
15,886,799 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to view board component and capacitor Pin
Amarnath S23-Mar-16 7:03
professionalAmarnath S23-Mar-16 7:03 
Questionhow to select all files automatically from zip file in c# Pin
Member 1237430822-Mar-16 7:00
Member 1237430822-Mar-16 7:00 
AnswerRe: how to select all files automatically from zip file in c# Pin
Richard MacCutchan22-Mar-16 7:23
mveRichard MacCutchan22-Mar-16 7:23 
AnswerRe: how to select all files automatically from zip file in c# Pin
koolprasad200322-Mar-16 18:21
professionalkoolprasad200322-Mar-16 18:21 
QuestionCrystal Reports Data not displaying Pin
prerana@200922-Mar-16 5:27
prerana@200922-Mar-16 5:27 
AnswerRe: Crystal Reports Data not displaying Pin
OriginalGriff22-Mar-16 6:03
mveOriginalGriff22-Mar-16 6:03 
AnswerRepost Pin
Richard Deeming22-Mar-16 6:27
mveRichard Deeming22-Mar-16 6:27 
QuestionHandling Cross thread Pin
pradipkilledar22-Mar-16 2:09
pradipkilledar22-Mar-16 2:09 
I have created thread list and started two thread
but I could not able to listbox even I had handled the cross thread issue. now once I run the app, I could not get the cross thread error, but app is hang and form also not displaying.

below is the code form1.cs:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Data.SqlClient;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Web;
using System.Xml;
using System.Net;



namespace GALAXY_LEAD_SERVER
{
    public partial class Form1 : Form
    {
        static int i = 0;
        static int corecamp = 0;
        static int cmpnID = 0;
        private Thread workerThread = null;
        private bool stopProcess = false;
        private delegate void UpdateStatusDelegate();
        private UpdateStatusDelegate updateStatusDelegate = null;
        CRCSMS crcsms = new CRCSMS();
        static int serverStatus = 0;
        public static string mystatus = "";
        static string AppLogFile = "SG_LeadServer";
        public static string logPath = Directory.GetCurrentDirectory() + "\\LOGS\\" + DateTime.Now.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture).Replace("/", "");
        SqlCommand cmd = new SqlCommand();
        GalaxyConfig GC = new GalaxyConfig();
        GalaxyLib GL = new GalaxyLib();
        LIBVALUE LB = new LIBVALUE();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            mystatus = "Load SG Lead Server " + version;
            this.updateStatusDelegate = new UpdateStatusDelegate(this.UpdateStatus);
            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
            this.Text = "SPECTRUM LEAD SERVER " + version;
            if (serverStatus == 0)
            {
                ServerStatustextBox.Text = "Stopped";
                Startbutton.Text = "Start";
            }
            else
            {
                Startbutton.Text = "Stop";
                ServerStatustextBox.Text = "Running";
            }
            mystatus = "Current Status: " + ServerStatustextBox.Text;

            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
        }
        //Thread t_thread1;
        //Thread t_thread2;
        class FileInfo
        {
            public int HostId { get; set; }
            public string Extension { get; set; }
            public string FolderPath { get; set; }
        }
        private void Startbutton_Click(object sender, EventArgs e)
        {
            var threads = new List<Thread>();
            string[] services = new string[2];
            services[0] = 1.ToString();
            services[1] = 2.ToString();





            if (serverStatus == 0)
            {
                AppLogFile = "SG_LeadServer";
                this.stopProcess = false;
                serverStatus = 1;
                Startbutton.Text = "Stop";
                ServerStatustextBox.Text = "Running";
                for (int i = 0; i <= services.Length - 1; i++)
                {
                    var hostID = (int)i;
                    FileInfo ff = new FileInfo();

                    var thread = new Thread(HeavyOperation)
                    {
                        Name = string.Format("myService{0}", i),
                    };
                    thread.Start(new FileInfo
                    {
                        HostId = hostID,
                    });
                    threads.Add(thread);
                    mystatus = "Added Thread: " + thread.Name.ToString();
                    //this.Invoke(this.updateStatusDelegate);
                    this.BeginInvoke(this.updateStatusDelegate);


                    threads[i].Join();
                    mystatus = "Joined Thread: " + thread.Name.ToString();
                    //this.Invoke(this.updateStatusDelegate);
                    this.BeginInvoke(this.updateStatusDelegate);

                }

                //this.workerThread = new Thread(new ThreadStart(this.HeavyOperation));
                //this.workerThread.Start();
                //this.workerThread = new Thread(new ThreadStart(this.HeavyOperation1));
                //this.workerThread.Start();

                //t_thread1 = new Thread(new ThreadStart(HeavyOperation));
                //t_thread1.Start();


                //t_thread2 = new Thread(new ThreadStart(HeavyOperation1));
                //t_thread2.Start();

            }
            else if (serverStatus == 1)
            {
                this.stopProcess = true;
                serverStatus = 0;
                LoadCampaigns();

                Startbutton.Text = "Start";
                ServerStatustextBox.Text = "Stopped";
                AppLogFile = "SG_LeadServer";
            }
            mystatus = "Current Status: " + ServerStatustextBox.Text;
            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeLabel.Text = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
        }
        private void HeavyOperation(object threadState)
        {
            
            for (i = 0; i <= 10; i++)
            {
                cmpnID = 8;
                Thread.Sleep(500);
                // Check if Stop button was clicked
                if (!this.stopProcess)
                {
                    // Show progress
                    if (serverStatus == 0)
                    {
                        mystatus = "Server stopped.contact administrator";
                        return;
                    }
                    LoadCampaigns();
                }
                else
                {
                    // Stop heavy operation
                    //this.workerThread.Abort();

                }
                if (i > 9) i = 0;
            }
        }
        private void HeavyOperation1()
        {
            
            //this.workerThread = new Thread(new ThreadStart(this.HeavyOperation1));
            for (i = 0; i <= 10; i++)
            {
                cmpnID = 10;
                Thread.Sleep(500);
                // Check if Stop button was clicked
                if (!this.stopProcess)
                {
                    // Show progress
                    if (serverStatus == 0)
                    {
                        mystatus = "Server stopped.contact administrator";
                        return;
                    }
                    LoadCampaigns();
                }
                else
                {
                    // Stop heavy operation
                    //this.workerThread.Abort();
                }
                if (i > 9) i = 0;
            }
        }
        private void UpdateStatus()
        {
            if (listBox1.Items.Count > 500) listBox1.Items.Clear();
            this.textBox1.Text = i.ToString();
            if (mystatus != "")
            {
                if(AppLogFile!="SG_LeadServer")
                    mystatus=LB.campaignName+"("+LB.campaignID+"): "+ mystatus;
                this.listBox1.Items.Insert(0, DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") + " ==> "+ mystatus);
                crcsms.writeLogFile(logPath, AppLogFile, mystatus);
            }
            mystatus = "";
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            mystatus = "App stop: " + ServerStatustextBox.Text;
            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
            this.workerThread.Abort();
            //aaa
        }
        private void LoadCampaigns()
        {
            if (serverStatus == 1)
            {
                LB.getCampaignDetails(cmpnID);
                AppLogFile = LB.campaignName;
                DataTable dt = new DataTable();
                dt = LB.getBatchDetails(LB.campaignID);
                //this.Invoke(this.updateStatusDelegate);
                this.BeginInvoke(this.updateStatusDelegate);
                int rowindex = 0;
                cmd.Connection = GC.getcon_Galaxy();
                //this.Invoke(this.updateStatusDelegate);
                this.BeginInvoke(this.updateStatusDelegate);
                foreach (DataRow row in dt.Rows)
                {
                    string strBatch = dt.Rows[rowindex][1].ToString();
                    mystatus = "Found Active Batch for campaign(" + LB.campaignID + ")" + LB.campaignName + ":-" + strBatch;
                    //this.Invoke(this.updateStatusDelegate);
                    this.BeginInvoke(this.updateStatusDelegate);
                    GalaxyLib.getCampaignDetails GL = new GalaxyLib.getCampaignDetails();
                    GL.updateMasterInsertQueue(LB.campaignID, strBatch,LB.outBoundDBName,LB.leadMasterTableName,LB.leadQueueTableName);
                    //this.Invoke(this.updateStatusDelegate);
                    this.BeginInvoke(this.updateStatusDelegate);
                    rowindex = rowindex + 1;
                }
            }
            else//SERVER STATUS 0
            {
                //GalaxyLib.getCampaignDetails GL = new GalaxyLib.getCampaignDetails();
                //GL.updateDeleteWhileStoppingServer(LB.outBoundDBName,LB.leadMasterTableName,LB.leadQueueTableName);
                //this.Invoke(this.updateStatusDelegate);
            }                
        }
        private void Stop_service1()
        {
            LB.getCampaignDetails(8);
            GalaxyLib.getCampaignDetails GL = new GalaxyLib.getCampaignDetails();
            GL.updateDeleteWhileStoppingServer(LB.outBoundDBName, LB.leadMasterTableName, LB.leadQueueTableName);
            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
            LB.getCampaignDetails(10);
            //GalaxyLib.getCampaignDetails GL = new GalaxyLib.getCampaignDetails();
            GL.updateDeleteWhileStoppingServer(LB.outBoundDBName, LB.leadMasterTableName, LB.leadQueueTableName);
            //this.Invoke(this.updateStatusDelegate);
            this.BeginInvoke(this.updateStatusDelegate);
        }

    }
}

AnswerRe: Handling Cross thread Pin
Richard Deeming22-Mar-16 2:58
mveRichard Deeming22-Mar-16 2:58 
GeneralRe: Handling Cross thread Pin
pradipkilledar27-Mar-16 22:22
pradipkilledar27-Mar-16 22:22 
GeneralRe: Handling Cross thread Pin
Richard Deeming29-Mar-16 1:45
mveRichard Deeming29-Mar-16 1:45 
QuestionGet Picture Using PictureBox Handle Pin
Django_Untaken21-Mar-16 7:29
Django_Untaken21-Mar-16 7:29 
AnswerRe: Get Picture Using PictureBox Handle Pin
User 740747021-Mar-16 8:43
User 740747021-Mar-16 8:43 
QuestionI want to generator serial numbers for rows in datagrid Pin
Engr. Yange21-Mar-16 3:06
Engr. Yange21-Mar-16 3:06 
AnswerRe: I want to generator serial numbers for rows in datagrid Pin
Eddy Vluggen22-Mar-16 7:17
professionalEddy Vluggen22-Mar-16 7:17 
QuestionUpdating the UI from a Rx background thread Pin
Kenneth Haugland20-Mar-16 23:38
mvaKenneth Haugland20-Mar-16 23:38 
AnswerRe: Updating the UI from a Rx background thread Pin
Luc Pattyn21-Mar-16 15:10
sitebuilderLuc Pattyn21-Mar-16 15:10 
GeneralRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 19:16
mvaKenneth Haugland21-Mar-16 19:16 
GeneralRe: Updating the UI from a Rx background thread Pin
Luc Pattyn21-Mar-16 19:35
sitebuilderLuc Pattyn21-Mar-16 19:35 
GeneralRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 20:24
mvaKenneth Haugland21-Mar-16 20:24 
AnswerRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 23:07
mvaKenneth Haugland21-Mar-16 23:07 
AnswerRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland22-Mar-16 23:56
mvaKenneth Haugland22-Mar-16 23:56 
QuestionIntermittent problem with design-time display of inherited UserControls in VS 2013 Pin
BillWoodruff19-Mar-16 4:36
professionalBillWoodruff19-Mar-16 4:36 
AnswerRe: Intermittent problem with design-time display of inherited UserControls in VS 2013 Pin
OriginalGriff19-Mar-16 5:23
mveOriginalGriff19-Mar-16 5:23 
GeneralRe: Intermittent problem with design-time display of inherited UserControls in VS 2013 Pin
BillWoodruff19-Mar-16 8:30
professionalBillWoodruff19-Mar-16 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.