0

I have been trying to modify an app developed by other in past ...

this app does online trading ( using api developed for C#)..so basically i have the app structure where i set few configuration paramter which user can check or uncheck and there is start and stop button

on clicking start button..i am creating a thread by passing function which will do all aping and other stuff and assigning it to main form class

betbot _mybot = this; # mybot is form class
this.main_thread = new Thread(new ThreadStart(_mybot.aping_function);
this.main_thread.Start();

and on clicking stop button,,,app is simply suspending the thread

this.main_thread.Suspend()

now the app stops and only way to resume the app function (aping) is to press start button and relaunch thread..

As a new feature , i want this thread to stop and restart automatically ..every time it hits certain stop loss and start over...but i couldn't do it

what i have tired is ManualResetEvent as following

private static ManualResetEvent mrse = new ManualResetEvent(true); 

when certain event matches in aping_function method i do mrse.reset() and mrse.set()..but that seems to have not effect( not restarting completely)

if (stop_loss_condition_met)
{
   this.Print1("Bot Is stopped Automatically");
   mrse.Reset();
   this.Print1("Bot Is re-started Automatically");
   mrse.Set();
}

how can i achieve this

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
MrPandav
  • 1,831
  • 1
  • 20
  • 24

1 Answers1

0

You should add a call of the WaitOne method at the place where your worker thread should stop.

mrse.WaitOne()

at the next time when external code reset it by call

mrse.Reset()

the execution will be stopped and thread will go to sleep at point

mrse.WaitOne()

until external code call Set method of the ManualResetEvent

mrse.Set()
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
  • So i side my aping_function i create a while(true) { mrse.WaitOne() and then inside my if condition if i will do mrse.reset() and mrse.set() in same order ..next to. Next...thread will stop there and restart from WaitOne line..right ? – MrPandav Aug 06 '15 at 15:46