Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
        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

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.
 
Share this answer
 
Comments
TheBigBearNow 4-Jun-20 9:39am    
That sounds like an awesome solution would you give me a lil example of what you mean ?
#realJSOP 4-Jun-20 12:05pm    
Are you using MVVM in your app?
TheBigBearNow 4-Jun-20 13:03pm    
Yes
#realJSOP 5-Jun-20 14:07pm    
Then you should have an idea already how to do what I suggested. In point of fact, you can use the subscribe to the object's propertychanged event from the property, and simply see if the desired property is what caused it to fire, and if so, do whatever you want to do.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900