Click here to Skip to main content
15,881,424 members
Home / Discussions / C#
   

C#

 
GeneralRe: Creating An Async Method Pin
Kevin Marois9-Oct-21 7:03
professionalKevin Marois9-Oct-21 7:03 
GeneralRe: Creating An Async Method Pin
lmoelleb10-Oct-21 8:56
lmoelleb10-Oct-21 8:56 
Question(xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED) Pin
Maximilien6-Oct-21 9:50
Maximilien6-Oct-21 9:50 
SuggestionRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard MacCutchan6-Oct-21 21:30
mveRichard MacCutchan6-Oct-21 21:30 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien7-Oct-21 3:49
Maximilien7-Oct-21 3:49 
AnswerRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard Deeming6-Oct-21 21:45
mveRichard Deeming6-Oct-21 21:45 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien8-Oct-21 3:03
Maximilien8-Oct-21 3:03 
QuestionCan't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor1-Oct-21 23:03
Exoskeletor1-Oct-21 23:03 
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out.

I'm using this code to run async function

private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
        {
            Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
                new Dictionary<string, string>()
                    {
                        {nameof(dateFrom), dateFrom.ToString()},
                        {nameof(dateTo), dateTo.ToString()},
                        {nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
                        {nameof(frequencyOption), frequencyOption.ToString()},
                        {nameof(fromDrawNumber), fromDrawNumber.ToString()},
                        {nameof(toDrawNumber), toDrawNumber.ToString()},
                    }
                );
            ((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
                GetString(Resource.String.Common_SearchMessage));

            //ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource());

            var task = Task.Run(async () =>
            {
                var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(textResult))
                {
                    UpdateHistoryList(textResult);
                    DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey);

                    ((MainActivity)Activity).ShowSearchResults(textResult);
                }
            }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run.

            try
            {
                await task.ConfigureAwait(false);
            }
            catch (TaskCanceledException tce)
            {
                Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}");
            }
            catch (System.ObjectDisposedException ode)
            {
                Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}");
            }
            catch (System.OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(System.OperationCanceledException)} thrown with message: {e.Message}");
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine($"{nameof(NullReferenceException)} thrown with message: {nre.Message}");
            }
            finally
            {
                ApplicationState.DisposeCancellationTokenSource();
                ((MainActivity)Activity).DisplayLoadingMessage(false);
            }

            ((MainActivity)Activity).DisplayLoadingMessage(false);
        }

But sometimes i get this exception

FragmentDrawsNumbersFrequency.SearchMostLessViewsAsync (System.DateTime dateFrom, System.DateTime dateTo, System.Int32 amountOfNumbersSelected, System.Int32 frequencyOption, System.Int32 fromDrawNumber, System.Int32 toDrawNumber)
FragmentDrawsNumbersFrequency.ShowMostLessViews ()
FragmentDrawsNumbersFrequency.<OnCreateView>b__50_8 (System.Object sender, System.EventArgs e)
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
SyncContext+<>c__DisplayClass2_0.<Post>b__0 ()
Thread+RunnableImplementor.Run ()
IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.61(intptr,intptr)

I'm not sure why. Shouldn't the try catch wrap catch nullreference exceptions? Something tells me i miss something else here

modified 2-Oct-21 8:56am.

AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon1-Oct-21 23:38
mvePete O'Hanlon1-Oct-21 23:38 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 0:33
mveOriginalGriff2-Oct-21 0:33 
JokeRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 1:32
mvePete O'Hanlon2-Oct-21 1:32 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 2:14
mveOriginalGriff2-Oct-21 2:14 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 3:01
Exoskeletor2-Oct-21 3:01 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 3:51
mvePete O'Hanlon2-Oct-21 3:51 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 3:55
mveOriginalGriff2-Oct-21 3:55 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 3:58
Exoskeletor2-Oct-21 3:58 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 2:58
Exoskeletor2-Oct-21 2:58 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon3-Oct-21 20:22
mvePete O'Hanlon3-Oct-21 20:22 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor4-Oct-21 0:21
Exoskeletor4-Oct-21 0:21 
Questionhow read file from Open with in C# Pin
Member 1522978130-Sep-21 6:24
Member 1522978130-Sep-21 6:24 
AnswerRe: how read file from Open with in C# Pin
Dave Kreskowiak30-Sep-21 8:08
mveDave Kreskowiak30-Sep-21 8:08 
AnswerRe: how read file from Open with in C# Pin
Luc Pattyn30-Sep-21 8:30
sitebuilderLuc Pattyn30-Sep-21 8:30 
QuestionQuestion About Async Pin
Kevin Marois29-Sep-21 10:21
professionalKevin Marois29-Sep-21 10:21 
AnswerRe: Question About Async Pin
Pete O'Hanlon29-Sep-21 20:22
mvePete O'Hanlon29-Sep-21 20:22 
GeneralRe: Question About Async Pin
Kevin Marois30-Sep-21 4:24
professionalKevin Marois30-Sep-21 4:24 

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.