Click here to Skip to main content
14,545,215 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello every1.
I have a program that is running and constantly updating an object property. I wasn’t even sure the best way to do this I did it with a Task.Run(). It’s checking a global ‘bool’ condition and it is started in the class constructor of the GUI. I’m updating a class property here and if it hits a certain price I would like to execute another method. I want it to execute code on that is in a button click() only if the price hits the condition. What do you think would be the best way to continously check this price condition? And execute the code in a button click

        public MainWindow()
        {
            InitializeComponent();
            
            dataTable = CreateDataTableTrades();

            _ = TradeAsync(dataTable);


        }
private async Task TradeAsync(DataTable dt)
        {
            while (true)
            {
                var delayTask = Task.Delay(1000);
                if (running)
                {
                    try
                    {
                       var response = await Task.Run(() => tradeClient.GetTradeHistory());
                        var stackTs = new Stack<ProductTrade>(response);
                        foreach(var trade in stackTs)
                        {
                            if(TradeID_NotExists(trade, dt))
                            {
                                CurrentTrade = UpdateCurrentTrade(trade, CurrentTrade);
                                
                                //PrintProdTrades(response);
                                PrintProductTrade(trade);
                                AddTradeToDt(trade, dt);
                                BindDT2DG(dt);
                            }
                        }
                        
                    }
                    catch (Exception e) { MessageBox.Show(e.Message); }
                }
                await delayTask;
            }
        }
private async void BtnTrade1_Sell_Click(object sender, RoutedEventArgs e)
        {
            string stopStr = "", limitStr = "", cryptoStr = "";
            bool isValid = false;

            if(!String.IsNullOrEmpty(txt1Sprice.Text) &&
                !String.IsNullOrEmpty(txt1Slimit.Text) &&
                !String.IsNullOrEmpty(txt1Scrypto.Text))
            {
                stopStr = txt1Sprice.Text;
                limitStr = txt1Slimit.Text;
                cryptoStr = txt1Scrypto.Text;
                StopOrders stopOrder = new StopOrders();
                bool stopBool=false, limitBool=false, cryptoBool=false;

                try
                {
                    if(decimal.TryParse(stopStr, out decimal stopPrice))
                    {
                        stopOrder.StopPrice = stopPrice;
                        stopBool = true;
                    }
                    if (decimal.TryParse(limitStr, out decimal limitPrice))
                    {
                        stopOrder.LimitPrice = limitPrice;
                        limitBool = true;
                    }
                    if (decimal.TryParse(cryptoStr, out decimal crypto))
                    {
                        stopOrder.CryptoAmount = crypto;
                        cryptoBool = true;
                    }

                    if(stopBool && limitBool && cryptoBool)
                    {
                        var response = await tradeClient.StopOrderSell(stopOrder);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Order Created Exception.\n" + ex.Message, "Error",
                                     MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }


What I have tried:

I have make a Task.run with a global bool variable in the GUI. I also had it working with a CancellationTokenSource but now I want to run a method in the background continuously until stopped and when a condition hits to run the code in a button click if the button has been enabled or turned on
Posted
Updated 24-May-20 3:36am
v2
Comments
RickZeeland 22-May-20 14:54pm
   
Task.Run is preferable over StartNew when using Async, see: https://blog.stephencleary.com/2013/08/startnew-is-dangerous.html

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Have you considered setting up the property to fire a custom event when its value changes, and then have an event handler in the form that reacts to the event? No threads needed.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100