question

shridhar avatar image
shridhar asked

Asynchronous services calling stored procedure after 2 sec

I have written stored procedure that check incoming data in table (table1) and insert into another table (table2) also update status of table (table1) for this I have written stored procedure Also I written windows service using c# for calling above stored procedure after 2 sec I have 2 service both working good for same work I want know that which one is good also suggest me required changes 1) private Task _proccessSmsQueueTask; private CancellationTokenSource _cancellationTokenSource; private ManualResetEvent _shutdownEvent; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { _shutdownEvent = new ManualResetEvent(false); _cancellationTokenSource = new CancellationTokenSource(); _proccessSmsQueueTask = Task.Run(() => DoWorkAsync(_cancellationTokenSource.Token)); } public async Task DoWorkAsync(CancellationToken token) { while (!_shutdownEvent.WaitOne(0)) { try { //calling procedure here } catch (Exception e) { // Handle exception } await Task.Delay(TimeSpan.FromSeconds(2), token); } } private void button2_Click(object sender, EventArgs e) { try { _shutdownEvent.Set(); _proccessSmsQueueTask.Wait(); } catch (Exception er) { // handle exeption } } } 2) private ManualResetEvent _shutdownEvent; private Thread _thread; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { _shutdownEvent = new ManualResetEvent(false); _thread = new Thread(method); _thread.Start(); } private void method() { while (!_shutdownEvent.WaitOne(0)) { // calling stored procedure Thread.Sleep(5000); } } private void button2_Click(object sender, EventArgs e) { _shutdownEvent.Set(); _thread.Join(); }
sql-server-2012c#windowsservice
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
I am not a C# guy at all. You might have better luck asking this question in a C# related forum, like Stack Overflow: https://stackoverflow.com/
0 Likes 0 ·

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.