首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#异步预测

C#异步预测
EN

Stack Overflow用户
提问于 2021-12-07 13:23:11
回答 1查看 362关注 0票数 -3

我正忙着在Windows窗体上开发一个股票跟踪应用程序。为了优化我的代码,我决定重写使所有内容都保持最新的主函数。我认为选择正确的行并从正确的行中工作将更容易,而不是为正确的控件设置所有的控件。问题是:它一直跳过我想要运行的任务,不再更新它。我尝试了很多我在网上发现的东西,但我无法让它起作用。所以我回到了最简单的原始代码的形式,看看你们是否有一个想法。这一切的实质是:

代码语言:javascript
复制
        public async void KeepUpdatingEverything(List<object> positionInfo, List<string> tickerList)
        {
            foreach (string ticker in tickerList)
            {
                //code that gets the right row
                List<object> priceInfo = await GetStockPrices(ticker));
                //code that updates all the labels
            }
        }

这样做的想法是,当调用KeepUpdating函数时,它会用代码检查列表,获取每个滴答的价格,然后更新所有相关标签。但是我似乎无法让它工作,因为它总是跳过异步调用。有什么想法吗?

当输入第一个代码时,KeepUpdatingEverything只被调用一次,之后它只会继续更新代码列表。

代码语言:javascript
复制
        private async void button1_Click(object sender, EventArgs e)
        {
            string ticker;
            using (Prompt prompt = new Prompt("Enter the ticker symbol", "Add ticker"))
            {
                ticker = prompt.Result;
                ticker = ticker.ToUpper();
                if (!string.IsNullOrEmpty(ticker))
                {

                    using (Prompt prompt2 = new Prompt("Enter your volume", "Add ticker"))
                    {
                        if (Int32.TryParse(prompt2.Result, out int volume) == true)
                        {
                            using (Prompt prompt3 = new Prompt("Enter your buy price", "Add ticker"))
                            {
                                if (Double.TryParse(prompt3.Result, out double buyPrice) == true)
                                {
                                    try
                                    {
                                        List<object> priceInfo = await GetStockPrices(ticker);
                                        FillTickerLabel(ticker);

                                        List<object> positionInfo = GetPositionVars(ticker, volume, buyPrice, Convert.ToDouble(priceInfo[1]));
                                        FillPositionLabel(ticker, priceInfo[0].ToString(), positionInfo);

                                        List<object> changeInfo = GetChangeVars(ticker, priceInfo);
                                        FillChangeLabels(ticker, priceInfo[0].ToString(), changeInfo);

                                        List<string> tickerList = new List<string>();
                                        tickerList.Add(ticker);
                                        if (tickerList.Count <= 1)
                                        {
                                            _cancellationToken = new CancellationTokenSource();
                                            _runningTask = StartTimer(() => KeepUpdatingEverything(positionInfo, tickerList), _cancellationToken);

                                        }
                                    }
                                    catch
                                    {
                                        MessageBox.Show("Ticker does not exist, or entered incorrect value somewhere else");
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("You did not enter one of the textboxes correctly");
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("You did not enter one of the textboxes correctly");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("You did not enter one of the textboxes correctly");
                }
            }
        }

最后,StartTimer函数:

代码语言:javascript
复制
        private async Task StartTimer(Action action, CancellationTokenSource cancellationTokenSource)
        {
            try
            {
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    await Task.Delay(5000, cancellationTokenSource.Token);
                    action();
                }
            }
            catch (OperationCanceledException) { }
        }
EN

回答 1

Stack Overflow用户

发布于 2021-12-07 13:27:29

async void意味着您不返回调用方可以await的任务。

你应该像这样使用async Task

代码语言:javascript
复制
public async Task KeepUpdatingEverything(List<object> positionInfo, List<string> tickerList)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70260920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档