Click here to Skip to main content
15,912,329 members
Home / Discussions / C#
   

C#

 
AnswerRe: "An assembly with the same simple name has already been imported" error message Pin
johannesnestler21-Jun-13 0:20
johannesnestler21-Jun-13 0:20 
GeneralRe: "An assembly with the same simple name has already been imported" error message Pin
impeham21-Jun-13 5:00
impeham21-Jun-13 5:00 
Questionhow do i implement barcode in C# Pin
Resma Rahiman17-Jun-13 0:44
Resma Rahiman17-Jun-13 0:44 
AnswerMy Vote of 1 Pin
Keith Barrow17-Jun-13 1:51
professionalKeith Barrow17-Jun-13 1:51 
QuestionError in WPF Pin
Nick_Frenk16-Jun-13 21:54
Nick_Frenk16-Jun-13 21:54 
AnswerRe: Error in WPF Pin
Keith Barrow16-Jun-13 23:02
professionalKeith Barrow16-Jun-13 23:02 
GeneralRe: Error in WPF Pin
Nick_Frenk16-Jun-13 23:56
Nick_Frenk16-Jun-13 23:56 
GeneralRe: Error in WPF Pin
Keith Barrow17-Jun-13 0:17
professionalKeith Barrow17-Jun-13 0:17 
C#
public void setImage(Image images,string url)
{
            
    if (images.Dispatcher.CheckAccess())
    {
        MessageBox.Show("ciao");
        Del ert = new Del(setImage);
            
        images.Dispatcher.Invoke(ert, new object[] { images, url }); //This calls setImage recursively
    }
    else
    {
        images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,  new object[] { images, url });  //This calls setImage recursively
    }
}


See the comment parts I've made in the stripped down version of your code. This is the problem, in both branches of the if/else you recursively try to call the method.
I think you need to do the following:
C#
public void setImage(Image images,string url)
        {
            
            if (images.Dispatcher.CheckAccess())
            {
                //Now on the dispatcher thread - can update the UI
                MessageBox.Show("ciao");
                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.UriSource = new Uri(url);
                img.EndInit(); // Getting exception here 
                images.Source = img;
            }
            else
            {
                //Not on the dispatcher thread call on dispatcher instead
                images.Dispatcher.BeginInvoke(new Del(setImage), DispatcherPriority.Normal,
                                                     new object[] { images, url });
            }
        }


Was should happen is setImage is called, if it is on the Dispatcher, fine, the UI is updated in the if block and the method exits. If the call isn't on the dispatcher the else block is executed, setImage calls itself on the dispatcher asychronously. When this call is executed, the if block is executed, the UI updated and the method exits (also exiting the call to setImage on the non-dispacther thread).
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed”
“One of the greatest problems of our time is that many are schooled but few are educated”


Sir Thomas More (1478 – 1535)

GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 0:28
Nick_Frenk17-Jun-13 0:28 
GeneralRe: Error in WPF Pin
Keith Barrow17-Jun-13 0:30
professionalKeith Barrow17-Jun-13 0:30 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 0:59
Nick_Frenk17-Jun-13 0:59 
GeneralRe: Error in WPF Pin
Keith Barrow17-Jun-13 1:44
professionalKeith Barrow17-Jun-13 1:44 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 2:43
Nick_Frenk17-Jun-13 2:43 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 2:26
mvePete O'Hanlon17-Jun-13 2:26 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 2:45
Nick_Frenk17-Jun-13 2:45 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 3:09
mvePete O'Hanlon17-Jun-13 3:09 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 3:18
Nick_Frenk17-Jun-13 3:18 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 3:31
mvePete O'Hanlon17-Jun-13 3:31 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 3:46
Nick_Frenk17-Jun-13 3:46 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 5:54
mvePete O'Hanlon17-Jun-13 5:54 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 21:22
Nick_Frenk17-Jun-13 21:22 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 21:27
mvePete O'Hanlon17-Jun-13 21:27 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 21:33
Nick_Frenk17-Jun-13 21:33 
GeneralRe: Error in WPF Pin
Pete O'Hanlon17-Jun-13 21:57
mvePete O'Hanlon17-Jun-13 21:57 
GeneralRe: Error in WPF Pin
Nick_Frenk17-Jun-13 22:17
Nick_Frenk17-Jun-13 22:17 

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.