Click here to Skip to main content
15,887,135 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
syedkhaleel20102-Nov-16 23:02
syedkhaleel20102-Nov-16 23:02 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 23:30
mvePete O'Hanlon2-Nov-16 23:30 
QuestionSQL injection prevention Pin
Member 128253812-Nov-16 8:28
Member 128253812-Nov-16 8:28 
AnswerRe: SQL injection prevention Pin
OriginalGriff2-Nov-16 9:46
mveOriginalGriff2-Nov-16 9:46 
AnswerRe: SQL injection prevention PinPopular
Richard Deeming2-Nov-16 10:10
mveRichard Deeming2-Nov-16 10:10 
AnswerRe: SQL injection prevention Pin
V.2-Nov-16 22:29
professionalV.2-Nov-16 22:29 
QuestionAsync Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 6:44
professionalFoothill1-Nov-16 6:44 
AnswerRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 7:48
mveRichard Deeming1-Nov-16 7:48 
Foothill wrote:
it seems to make the code look a lot more complicated

That's because you've significantly over-complicated it! Smile | :)
C#
private btn_Submit_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MainWindow.SetCursor_Wait();
        if (InputsAreGood())
        {
            DataCollection data = GetFormFields():
            await PushDataToDatabaseAsync(data);
            ClosePopOutForm();
        }
        else
        {
            // tell user what fields they missed
        }
    }
    catch (Exception ex)
    {
        // display the error to the user
    }
    finally
    {
        MainWindow.SetCusor_Arrow();
    }
}

private async Task PushToDatabaseAsync(DataCollection data)
{
    // Let any exceptions bubble up the call-stack to be displayed by the calling method.
    // TODO: Make this a real async call to the database if possible.
    await Task.Run(() => PerformDatabaseInsert(data));
    
    // Using "await" means we're back on the UI thread here,
    // so there should be no need to use the Dispatcher.
    if (Dispatcher.CheckAccess())
    {
        UpdateList(data)
    }
    else
    {
        await Dispatcher.BeginInvoke((Action<DataCollection>)UpdateList, data);
    }
}

private void UpdateList(DataCollection data)
{
    lst_MyData.ItemsSource = data.ToArray();
}

NB: If at all possible, you should make your PerformDatabaseInsert method async, using the built-in async methods on the DbConnection / DbCommand types. If you're using a DataAdapter, there's no async support, so you're stuck with pushing the update onto a background thread.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 8:12
professionalFoothill1-Nov-16 8:12 
GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 8:23
mveRichard Deeming1-Nov-16 8:23 
QuestionLabel printing with c# Pin
candogu1-Nov-16 0:57
candogu1-Nov-16 0:57 
AnswerRe: Label printing with c# Pin
OriginalGriff1-Nov-16 1:44
mveOriginalGriff1-Nov-16 1:44 
AnswerRe: Label printing with c# Pin
Richard Deeming1-Nov-16 3:37
mveRichard Deeming1-Nov-16 3:37 
AnswerRe: Label printing with c# Pin
cinias8-Nov-16 3:49
cinias8-Nov-16 3:49 
QuestionHow people generate bench mark graph for c# routine execution speed Pin
Tridip Bhattacharjee31-Oct-16 19:08
professionalTridip Bhattacharjee31-Oct-16 19:08 
AnswerRe: How people generate bench mark graph for c# routine execution speed Pin
Pete O'Hanlon31-Oct-16 20:52
mvePete O'Hanlon31-Oct-16 20:52 
QuestionHow to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 0:29
Rıza Berkay Ayçelebi31-Oct-16 0:29 
AnswerRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon31-Oct-16 0:50
mvePete O'Hanlon31-Oct-16 0:50 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 23:14
Rıza Berkay Ayçelebi31-Oct-16 23:14 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 0:54
mveRichard MacCutchan1-Nov-16 0:54 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi1-Nov-16 1:36
Rıza Berkay Ayçelebi1-Nov-16 1:36 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 1:53
mveRichard MacCutchan1-Nov-16 1:53 
GeneralRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon1-Nov-16 2:30
mvePete O'Hanlon1-Nov-16 2:30 
AnswerRe: How to implement the huffman using c# for image Pin
Jochen Arndt31-Oct-16 1:48
professionalJochen Arndt31-Oct-16 1:48 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 3:19
Rıza Berkay Ayçelebi31-Oct-16 3:19 

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.