Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. namespace MainApp
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public static Server socket = null;
  6.         public static ManualResetEvent _shutdownEvent;
  7.         public static Thread _thread, thread2;
  8.        
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         public class Server : WebSocketBehavior
  15.         {
  16.             Form1 frm = new Form1();
  17.             FingerprintServer.Form1 fp = new FingerprintServer.Form1();
  18.             TimeSpan waitTime = new TimeSpan(0, 0, 7);
  19.             DateTime waitUntil;
  20.             CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
  21.             CancellationToken token = new CancellationToken();
  22.             Task task2;
  23.  
  24.             protected override void OnMessage(MessageEventArgs e)
  25.             {
  26.                 var msg = e.Data;
  27.                 waitUntil = DateTime.Now + waitTime;
  28.                 switch (msg)
  29.                 {
  30.                     case "InitScanner":
  31.                         //fp.StopBiometricScanner();
  32.                         msg = FingerprintServer.Form1.InitBiometricScanner();
  33.                         Send(msg);
  34.                         break;
  35.                     case "StartScanning":
  36.                         task2 = Task.Factory.StartNew(() =>
  37.                         {
  38.                            
  39.                             while (DateTime.Now < waitUntil)
  40.                             {
  41.                                 if (token.IsCancellationRequested)
  42.                                 {
  43.                                     Console.WriteLine("Операция прервана");
  44.                                     return;
  45.                                 }
  46.                                 Send(fp.DoWork());
  47.                             }
  48.                         });                                            
  49.                         break;
  50.                     case "StopScanning":
  51.                         cancelTokenSource.Cancel(); //по вэб сокету приходит флаг StopScanning. Но он не хочет останавливать поток
  52.                         break;
  53.                 }
  54.  
  55.             }
  56.  
  57.         }
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement