Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
GeneralRe: Reading Xml to and fro Pin
squerley13-Dec-10 5:58
squerley13-Dec-10 5:58 
GeneralRe: Reading Xml to and fro Pin
RaviRanjanKr13-Dec-10 6:05
professionalRaviRanjanKr13-Dec-10 6:05 
GeneralRe: Reading Xml to and fro Pin
squerley13-Dec-10 6:25
squerley13-Dec-10 6:25 
GeneralRe: Reading Xml to and fro Pin
squerley13-Dec-10 6:13
squerley13-Dec-10 6:13 
GeneralRe: Reading Xml to and fro Pin
RaviRanjanKr13-Dec-10 6:19
professionalRaviRanjanKr13-Dec-10 6:19 
GeneralRe: Reading Xml to and fro Pin
ShilpaKumari13-Dec-10 6:28
ShilpaKumari13-Dec-10 6:28 
GeneralRe: Reading Xml to and fro [modified] Pin
squerley13-Dec-10 6:37
squerley13-Dec-10 6:37 
QuestionCPU jumps from 16% to 78% Pin
Jassim Rahma13-Dec-10 2:47
Jassim Rahma13-Dec-10 2:47 
Hi,

I have this code which works fine but there is a major issue specially i am going t run this on the server! when the code in the timer start running, the CPU usage of the machine jumps from 16% to 78%? of course after completed it will go back to normal but my question. what's happening in the code? why it take this much from CPU?


MySqlConnection sql_connection = null;
MySqlCommand sql_command = null;
MySqlDataAdapter sql_adapter;
DataTable data_table = null;
MySqlDataReader sql_reader = null;
DataSet data_set = null;

public public_class public_var = new public_class();

const int MF_BYPOSITION = 0x400;

[DllImport("User32.dll")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlagS);

[DllImport("User32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("User32.dll")]
private static extern int GetMenuItemCount(IntPtr hWnd);

public delegate void ControlStringConsumer(Control control, string text);

public int result_rows;

public frmMain()
{
    InitializeComponent();
}

public void SetText(Control control, string text)
{
    control.Invoke(new ControlStringConsumer(SetTextGUI), new object[] { control, text });  // invoking another method
}

public void SetTextGUI(Control control, string text)
{
    control.Text = text;      // the "functional part", acceptable only on the main thread
}

private void perform()
{
    get_statistics_summary();
    send_sms();
}

private void get_statistics_summary()
{
    // progressRunning.Visible = true;

    // try
    // {
        // sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=shefa;integrated security=true");
        sql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString);
        sql_connection.Open();
        sql_command = new MySqlCommand("SELECT COUNT(*) AS total_files FROM persons", sql_connection);
        sql_command.CommandType = CommandType.Text;
        sql_reader = sql_command.ExecuteReader();

        if (sql_reader.Read())
        {
            // lblTotalFiles.Text = sql_reader["total_files"].ToString();
            SetText(lblTotalFiles, sql_reader["total_files"].ToString());
        }

        sql_reader.Close();

        sql_command = new MySqlCommand("SELECT COUNT(*) AS total_appointments FROM appointments", sql_connection);
        sql_command.CommandType = CommandType.Text;
        sql_reader = sql_command.ExecuteReader();

        if (sql_reader.Read())
        {
            // lblTotalAppointments.Text = sql_reader["total_appointments"].ToString();
            SetText(lblTotalAppointments, sql_reader["total_appointments"].ToString());
        }
    /*
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
    }
    finally
    {
        if (sql_reader != null) sql_reader.Close();
        if (sql_connection != null)
        {
            if (sql_connection.State == ConnectionState.Open)
                sql_connection.Close();
        }
    }
     * */

    // progressRunning.Visible = false;
}

private void send_sms()
{
    // progressRunning.Visible = true;

    // try
    // {
        sql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString);
        sql_connection.Open();
        sql_command = new MySqlCommand("sp_send_pending_sms", sql_connection);
        sql_command.CommandType = CommandType.StoredProcedure;
        sql_reader = sql_command.ExecuteReader();

        while (sql_reader.Read())
        {
            // lblTimer.Text = sql_reader["sms_queue_id"].ToString();
            SetText(lblTimer, sql_reader["sms_queue_id"].ToString());
        }
    /*
    }
    catch (Exception exp)
    {
        MessageBox.Show(exp.Message);
    }
    finally
    {
        if (sql_reader != null) sql_reader.Close();
        if (sql_connection != null)
        {
            if (sql_connection.State == ConnectionState.Open)
                sql_connection.Close();
        }
    }
     * */

    // progressRunning.Visible = false;
}

private void frmMain_Load(object sender, EventArgs e)
{
    this.Cursor = Cursors.WaitCursor;

    IntPtr hMenu = GetSystemMenu(this.Handle, false);
    int menuItemCount = GetMenuItemCount(hMenu);
    RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

    timerMain.Interval = Convert.ToInt32(ConfigurationManager.AppSettings["ServerTimerInterval"]);

    // public_var.get_system_properties();
}

private void btnEmailTemplates_Click(object sender, EventArgs e)
{
    frmEmailTemplates EmailTemplatesForm = new frmEmailTemplates(public_var);
    EmailTemplatesForm.ShowDialog();
}

private void frmMain_Shown(object sender, EventArgs e)
{
    this.Cursor = Cursors.WaitCursor;

    timerMain.Start();

    this.Cursor = Cursors.Default;
}


private void timerMain_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // lblTimer.Text = DateTime.Now.ToString();
    backgroundWorkerSMS.RunWorkerAsync();

    // send_sms();
}

private void btnOptions_Click(object sender, EventArgs e)
{
    frmOptions OptionsForm = new frmOptions();
    DialogResult options_form = OptionsForm.ShowDialog();

    if (options_form == DialogResult.OK)
    {
        timerMain.Stop();

        timerMain.Interval = Convert.ToInt32(ConfigurationManager.AppSettings["ServerTimerInterval"]);

        timerMain.Start();
    }
}

private void backgroundWorkerSMS_DoWork(object sender, DoWorkEventArgs e)
{
    perform();
}

private void backgroundWorkerSMS_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    backgroundWorkerSMS.Dispose();
}
</pre>

AnswerRe: CPU jumps from 16% to 78% Pin
Luc Pattyn13-Dec-10 3:16
sitebuilderLuc Pattyn13-Dec-10 3:16 
GeneralRe: CPU jumps from 16% to 78% Pin
Jassim Rahma13-Dec-10 3:27
Jassim Rahma13-Dec-10 3:27 
QuestionRe: CPU jumps from 16% to 78% Pin
Luc Pattyn13-Dec-10 3:34
sitebuilderLuc Pattyn13-Dec-10 3:34 
GeneralRe: CPU jumps from 16% to 78% Pin
Dave Kreskowiak13-Dec-10 8:54
mveDave Kreskowiak13-Dec-10 8:54 
AnswerRe: CPU jumps from 16% to 78% Pin
fjdiewornncalwe13-Dec-10 8:32
professionalfjdiewornncalwe13-Dec-10 8:32 
GeneralRe: CPU jumps from 16% to 78% Pin
Jassim Rahma14-Dec-10 2:32
Jassim Rahma14-Dec-10 2:32 
QuestionWindow open and close effect with c# in windows application Pin
Tridip Bhattacharjee13-Dec-10 2:13
professionalTridip Bhattacharjee13-Dec-10 2:13 
AnswerRe: Window open and close effect with c# in windows application Pin
PIEBALDconsult13-Dec-10 2:18
mvePIEBALDconsult13-Dec-10 2:18 
GeneralRe: Window open and close effect with c# in windows application Pin
Tridip Bhattacharjee13-Dec-10 19:20
professionalTridip Bhattacharjee13-Dec-10 19:20 
GeneralRe: Window open and close effect with c# in windows application Pin
SilimSayo17-May-11 6:56
SilimSayo17-May-11 6:56 
Questionhelp!how can i get the monitor's status Pin
scoket12-Dec-10 22:35
scoket12-Dec-10 22:35 
AnswerRe: help!how can i get the monitor's status Pin
Prerak Patel12-Dec-10 23:01
professionalPrerak Patel12-Dec-10 23:01 
GeneralRe: help!how can i get the monitor's status Pin
scoket13-Dec-10 15:05
scoket13-Dec-10 15:05 
AnswerRe: help!how can i get the monitor's status Pin
PIEBALDconsult13-Dec-10 2:19
mvePIEBALDconsult13-Dec-10 2:19 
GeneralRe: help!how can i get the monitor's status Pin
OriginalGriff13-Dec-10 2:29
mveOriginalGriff13-Dec-10 2:29 
GeneralRe: help!how can i get the monitor's status Pin
Roger Wright14-Dec-10 18:41
professionalRoger Wright14-Dec-10 18:41 
QuestionExcel receive live data from DDE .How to retrieve those values Pin
mladenovic12-Dec-10 22:19
mladenovic12-Dec-10 22:19 

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.