Click here to Skip to main content
15,558,870 members
Home / Discussions / C#
   

C#

 
QuestionSimple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 14:32
Member 149309634-Sep-20 14:32 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
Mycroft Holmes4-Sep-20 16:19
professionalMycroft Holmes4-Sep-20 16:19 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 16:21
Member 149309634-Sep-20 16:21 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Mycroft Holmes4-Sep-20 16:57
professionalMycroft Holmes4-Sep-20 16:57 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 17:56
Member 149309634-Sep-20 17:56 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
OriginalGriff4-Sep-20 21:07
mveOriginalGriff4-Sep-20 21:07 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
Eddy Vluggen5-Sep-20 6:27
professionalEddy Vluggen5-Sep-20 6:27 
QuestionRunning Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor4-Sep-20 13:41
Exoskeletor4-Sep-20 13:41 
I have this function

private async Task<int> SearchForAllNumbers(int num = -1)
       {
           if (num == -1)
               DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle), GetString(Resource.String.Common_SearchMessage));
           dates = DateFunctions.GetSelectedDates(DateFrom, DateTo);

           var list = string.Empty;
           var klirwseis = ", Κληρωσεις: ";
           if (num == -1)
               list = ListTextView.Text;
           else
               list = num.ToString();
           var nums = list.Split(',').Select(Int32.Parse).ToList();
           if (seperateCheckBox.Checked)
           {
               DisplayLoadingMessage(false);
               seperateCheckBox.Checked = false;
               for (int k = 0; k < nums.Count; k++)
               {
                   await SearchForAllNumbers(nums[k]);
               }
               return -1;
           }
           var totalCoutner = 0;
           for (int datesPos = 0; datesPos <= dates.Count; datesPos++)
           {
              ... Do some calculations
               }
               HistoryTextView.Text = "Ημερομηνία: " + dates[datesPos] + ", Λίστα: " + list + ", Κληρώθηκε: " + totalCoutner + " φορές" + HistoryTextView.Text;
               if (showListsCheckBox.Checked)
                   HistoryTextView.Text = klirwseis + HistoryTextView.Text;
           }
           DisplayLoadingMessage(false);
           HistoryTextView.Text = ".\n" + HistoryTextView.Text;
           return totalCoutner;
           return 0;
       }


And i want to be able to cancel it when its running so i have created this function

private async Task SearchFoNumbersAsync()
        {
            ActiveCancellationTokenSource = new CancellationTokenSource();
            DrawResultsTypeEnum searchType = (DrawResultsTypeEnum)ShowResultsSpinnerPosition;

            var task = Task.Run(async () =>
            {
                if (searchType == DrawResultsTypeEnum.AllNumbers)
                    await SearchForAllNumbers();
                else if (searchType == DrawResultsTypeEnum.AnyWinningCombination)
                    await SearchAnyWinningCombination();
                SaveHistory();
            }, ActiveCancellationTokenSource.Token); // Pass same token to Task.Run.

            try
            {
                await task;
            }
            catch (System.OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(System.OperationCanceledException)} thrown with message: {e.Message}");
                DisplayLoadingMessage(false);
            }
            finally
            {
                ActiveCancellationTokenSource.Dispose();
                DisplayLoadingMessage(false);
            }
        }


To be able to run it from Task.Run and to be able to cancel it. The code doesnt work however
I have debug it and when it reach the
try
            {
                await task;
            }

Stays there forever, the
SearchForAllNumbers()
or
SearchAnyWinningCombination()
are not accessed at all.
i'm not sure what i have done wrong.
AnswerRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 5:31
mveGerry Schmitz5-Sep-20 5:31 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 9:25
Exoskeletor5-Sep-20 9:25 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 10:05
mveGerry Schmitz5-Sep-20 10:05 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 11:17
Exoskeletor5-Sep-20 11:17 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 11:38
mveGerry Schmitz5-Sep-20 11:38 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 11:58
Exoskeletor5-Sep-20 11:58 
Question.Net Core Windows App, using database repositories Pin
jkirkerx4-Sep-20 12:53
professionaljkirkerx4-Sep-20 12:53 
AnswerRe: .Net Core Windows App, using database repositories Pin
Mycroft Holmes4-Sep-20 13:07
professionalMycroft Holmes4-Sep-20 13:07 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx4-Sep-20 14:45
professionaljkirkerx4-Sep-20 14:45 
GeneralRe: .Net Core Windows App, using database repositories Pin
Mycroft Holmes4-Sep-20 16:14
professionalMycroft Holmes4-Sep-20 16:14 
GeneralRe: .Net Core Windows App, using database repositories Pin
Gerry Schmitz5-Sep-20 5:40
mveGerry Schmitz5-Sep-20 5:40 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx5-Sep-20 8:32
professionaljkirkerx5-Sep-20 8:32 
GeneralRe: .Net Core Windows App, using database repositories Pin
Gerry Schmitz5-Sep-20 9:46
mveGerry Schmitz5-Sep-20 9:46 
AnswerRe: .Net Core Windows App, using database repositories Pin
Richard Deeming6-Sep-20 23:30
mveRichard Deeming6-Sep-20 23:30 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 7:58
professionaljkirkerx7-Sep-20 7:58 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 10:14
professionaljkirkerx7-Sep-20 10:14 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 11:29
professionaljkirkerx7-Sep-20 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.