Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void RefreshThread()
- {
- var watch = Stopwatch.StartNew();
- while (true)
- {
- UpdateAllNetworks();
- UpdateAllInterferences();
- UpdateAllColors();
- switch (ActivePage)
- {
- case Page.Start:
- break;
- case Page.Networks:
- this.Invoke((MethodInvoker)delegate
- {
- UpdateDataGridWithNetworks();
- ClearGraphs();
- Draw24GHzGraph();
- DrawSignalsOverTimeGraph();
- });
- break;
- case Page.Channels:
- break;
- case Page.Analyze:
- break;
- default:
- break;
- }
- watch.Stop();
- int elapsedMs = (int) watch.ElapsedMilliseconds;
- if (elapsedMs < Constants.NetworksRefreshThreadInterval)
- Thread.Sleep(Constants.NetworksRefreshThreadInterval - elapsedMs);
- }
- }
- public class CustomDataGridView : DataGridView
- {
- ...
- protected override void OnCellClick(DataGridViewCellEventArgs e)
- {
- base.OnCellClick(e);
- int Index = e.RowIndex;
- if (Index != -1)
- {
- DataGridViewRow row = Rows[Index];
- PrimaryKeyForSelectedRow = row.Cells[KeyName].Value.ToString();
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var helper = new Looper(5000, YourMethod_RefreshThread);
- helper.Start();
- }
- private static void YourMethod_RefreshThread()
- {
- Console.WriteLine(DateTime.Now);
- }
- }
- public class Looper
- {
- private readonly Action _callback;
- private readonly int _interval;
- public Looper(int interval, Action callback)
- {
- if(interval <=0)
- {
- throw new ArgumentOutOfRangeException("interval");
- }
- if(callback == null)
- {
- throw new ArgumentNullException("callback");
- }
- _interval = interval;
- _callback = callback;
- }
- private void Work()
- {
- var next = Environment.TickCount;
- do
- {
- if (Environment.TickCount >= next)
- {
- _callback();
- next = Environment.TickCount + _interval;
- }
- Thread.Sleep(_interval);
- } while (IsRunning);
- }
- public void Start()
- {
- if (IsRunning)
- {
- return;
- }
- var thread = new Thread(Work);
- thread.Start();
- IsRunning = true;
- }
- public void Stop()
- {
- this.IsRunning = false;
- }
- public bool IsRunning { get; private set; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement