Keywords: delegate-thread asynchronous operation large data stored procedure paging real time Refresh interface Data Voice alarm taskbar prompt dynamic taskbar icon Toggle
requirements: After starting the monitor, every 10 seconds (how many seconds can be configured) from the background database query once,
Query criteria (SQL statements can be configured), and then return the results to display and make judgments.
Data inserted immediately: (first column)
This example source code program
The conditions of judgment (as far as possible flexible configuration): The return of the data inside, the number of empty 2 greater than 0 times,
You need to interval 1 seconds to take again, if found or 2 multiples large, then the alarm,
If a multiple of 0 is greater than 2, you can query once every 10 seconds (configurable)
Program Interface:
Configuration:
Implementation code:
Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Data;
Using System.Drawing;
Using System.IO;
Using System.Runtime.InteropServices;
Using System.Threading;
Using System.Windows.Forms;
Namespace Smmmmonitoring
{
public partial class Form1:form
{
private static string strconn = configurationmanager.appsettings["Constr"];;
private static string strprocedure = configurationmanager.appsettings["ProcName"];
private static int pageindex = Convert.ToInt32 (configurationmanager.appsettings["pageindex")); Refresh first few pages
private static int multiples = Convert.ToInt32 (configurationmanager.appsettings["multiple")); Alarm Multiples
Storeprocedure sa = new Storeprocedure (strprocedure, strconn);
Public Form1 ()
{
InitializeComponent ();
BINDDGV ();
}
private void Binddatawithpage (int Index)
{
Winformpager1.pageindex = Index;
string table = configurationmanager.appsettings["Table"];
string field = configurationmanager.appsettings["Field"];
string by = configurationmanager.appsettings["by"];
string where = configurationmanager.appsettings["where"];
DataTable dt = sa. Executedatatable (table, field, by, by, Index, 0, 0, where);
Datagridview1.datasource = DT;
Winformpager1.recordcount = Convert.ToInt32 (sa. Executedatatable (table, field, by, by, Index, 1, 0, where). rows[0] [0]);
}
private void Winformpager1_pageindexchanged (object sender, EventArgs e)
{
Binddatawithpage (Winformpager1.pageindex);
}
Invalid thread operation: It is not accessed from a thread that is not creating a control "DataGridView1".
Call: BINDDGV (9,binddatawithpage); Passing parameters
/* Private delegate void binddgvdelegate (int n);
private void Binddgv (int n, binddgvdelegate mydelegate)
{
if (this. invokerequired)
{
This. Invoke (MyDelegate, n);//Sync
}
Else
{
MyDelegate (n);
}
}
*/
/*
The Invoke and BeginInvoke delegate method of control is executed on the main thread, the UI thread.
* That is, if your delegate method is used to take a long amount of data and then update the interface or something,
* Never invoke Control.Invoke and Control.BeginInvoke on the UI thread, as opposed to the invokethread thread, because these are still blocking UI threads, causing the interface to suspend animation.
* Use thread to invoke BeginInvoke and invoke
* Order of execution: A---BC (b and C are executed simultaneously, B executes on the thread UI, C executes the thread begininvokethread on the line)--de
*/
Private Thread Begininvokethread;
Private delegate void Begininvokedelegate ();
private void Startmethod ()
{
C Code Snippet ... Long time consuming operations
Datagridview1.begininvoke (New Begininvokedelegate (Begininvokemethod));
D Code Snippet ... Delete
}
private void Begininvokemethod ()
{
E Code Snippet Update interface method.
Binddatawithpage (pageindex);
}
private void BINDDGV ()
{
A code snippet .....
Begininvokethread = new Thread (new ThreadStart (Startmethod));
Begininvokethread.start ();
B Code Snippet ...
}
<summary>
The alarm is greater than how many times
</summary>
<param name= "DGV" ></param>
<param name= "n" ></param>
public bool Alarm (DataGridView DGV, int n)
{
int space = 0; int Success = 0;
Dictionary < string, int > dict = dictionarycolumns (DGV);
foreach (KeyValuePair < string, int > kvp in dict)
{
if (kvp. Key = = "2")
{
Space = kvp. Value;
}
if (kvp. Key = = "0")
{
Success = kvp. Value;
}
}
if (Space >= Success * N)
{
Alarm
MessageBox.Show ("alarm.") "," Information Tips ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
Else
{
MessageBox.Show ("bu alarm.") "," Information Tips ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}
Public Dictionary < string, int > dictionarycolumns (DataGridView DGV)
{
Dictionary < string, int > DCT = new Dictionary < string, int > ();
String str = "";
for (int i = 0; i < DGV. rows.count-1; i + +)
{
str = convert.tostring (DGV. Rows[i]. cells[4]. Value);
if (DCT. ContainsKey (str))//If this key already exists in the dictionary, add 1 to the key value
{
DCT[STR] + +;
}
Else
{
Dct. ADD (str, 1); This key is not present in the dictionary, join this key
}
}
return DCT;
}
private datetime Lastrefresh = new datetime (0);
private int refreshinterval = 10;
private string lasttitle = String. Empty;
private string lastmessage = String. Empty;
<summary>
Update data
</summary>
public void Refreshallmessages ()
{
BINDDGV ();
}
<summary>
Update Upcoming Events
</summary>
private void Refreshasync ()
{
Update last Execution time
Updatelastrefreshtime ();
Establish a state of inquiry
Buildmessagesquerying ();
Asynchronous update in order to not get stuck