NullReferenceException lors d'un AsyncCallback

cs_AmK Messages postés 368 Date d'inscription jeudi 13 mars 2003 Statut Membre Dernière intervention 27 janvier 2010 - 25 avril 2008 à 20:56
cs_AmK Messages postés 368 Date d'inscription jeudi 13 mars 2003 Statut Membre Dernière intervention 27 janvier 2010 - 25 avril 2008 à 21:03
Bonsoir,

je désire faire un appel asynchrone vers un webservice mappoint, j'opère donc de la manière suivante:

   Stream imageStream; // variable globale
   void DisplayMap(double latitude,double longitude)
   {
       // du code...
       RenderServiceSoap rss;
       rss.BeginGetMap(mapSpecif, new AsyncCallback(GetMapThread), rss);
   }

         private void GetMapThread(IAsyncResult state)
        {
            RenderServiceSoap rss = state.AsyncState as RenderServiceSoap;
            if (rss == null)
                return;
            MapImage[] images = rss.EndGetMap(state);
            imageStream = new System.IO.MemoryStream(images[0].MimeData.Bits);

        }

       // puis au niveau de ma fct principale
       DisplayMap(pos_lat,pos_long);
       pictureBox1.Image=new System.Drawing.Bitmap(imageStream); //la varia ble globale devrait être mis à jour à l'intérieur de GetMapThread


A l'execution j'obtiens une exception de type NullReferenceException, donc voici le détail :
        StackTrace    "   at System.Drawing.Bitmap..ctor(Stream stream)\r\n   at PickMeUp.RiderStartSearch..ctor()\r\n   at PickMeUp.RiderMainMenu.startsearch_SelectedIndexChanged(Object sender, EventArgs e)\r\n   at System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e)\r\n   at System.Windows.Forms.ListView.UpdateSelectedIndex(Int32 iItem)\r\n   at System.Windows.Forms.ListView.WnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)\r\n   at System.Windows.Forms.Form.ShowDialog()\r\n   at PickMeUp.SelectMode.RiderMode_SelectedIndexChanged(Object sender, EventArgs e)\r\n   at System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e)\r\n   at System.Windows.Forms.ListView.UpdateSelectedIndex(Int32 iItem)\r\n   at System.Windows.Forms.ListView.WnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)\r\n   at System.Windows.Forms.Form.ShowDialog()\r\n   at PickMeUp.FLogin.Login_Click(Object sender, EventArgs e)\r\n   at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)\r\n   at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n   at System.Windows.Forms.Application.Run(Form fm)\r\n   at PickMeUp.Program.Main()\r\n"    string

Ce qui est bizarre c'est qu'un coup j'obtiens ça, un coup j'obtiens un NullReferenceException mais avec un message comme quoi je devrais utiliser Invoke..
"Control.Invoke must be used to interact with controls created on a separate thread."    string
Je tiens à signaler que j'ai tenté d'utiliser un delegate de la manière suivante :

        private delegate void GetMapThreadDelegate(IAsyncResult state);
        private void DoUpdate2(IAsyncResult state)
        {

            if (this.InvokeRequired)
            {
     
                this.Invoke(new GetMapThreadDelegate(DoUpdate2),new object[] { state });
                return;
            }
         
            MapImage[] images = rss.EndGetMap(state);
            imageStream = new System.IO.MemoryStream(images[0].MimeData.Bits);

        }


Avec la mise à jour effectuée dans GetMapThread ( qui appelle la méthode DoUpdate2 )

J'ai cherché sur le net de l'aide sur ce genre de msg d'erreur apparemment c'est assez connu comme problème, mais je n'ai pas su adapter les solutions trouvées à mon cas :(

Merci pour votre aide grandement appréciée !

1 réponse

cs_AmK Messages postés 368 Date d'inscription jeudi 13 mars 2003 Statut Membre Dernière intervention 27 janvier 2010 1
25 avril 2008 à 21:03
PS:desolé pour la mise en forme j'étais pas au courant que les balises quote et code ne fonctionnaient pas ici, et pas moyen d'éditer apparemment ?  Je reposte en ajoutant des couleurs ^^

Bonsoir,

je désire faire un appel asynchrone vers un webservice mappoint, j'opère donc de la manière suivante:

   Stream imageStream; // variable globale
   void DisplayMap(double latitude,double longitude)
   {
       // du code...
       RenderServiceSoap rss;
       rss.BeginGetMap(mapSpecif, new AsyncCallback(GetMapThread), rss);
   }

         private void GetMapThread(IAsyncResult state)
        {
            RenderServiceSoap rss = state.AsyncState as RenderServiceSoap;
            if (rss == null)
                return;
            MapImage[] images = rss.EndGetMap(state);
            imageStream = new System.IO.MemoryStream(images[0].MimeData.Bits);

        }

       // puis au niveau de ma fct principale
       DisplayMap(pos_lat,pos_long);
      
pictureBox1.Image=new System.Drawing.Bitmap(imageStream); //la varia
ble globale devrait être mis à jour à l'intérieur de GetMapThread

A l'execution j'obtiens une exception de type NullReferenceException, donc voici le détail :

   
    StackTrace    "   at System.Drawing.Bitmap..ctor(Stream
stream)\r\n   at PickMeUp.RiderStartSearch..ctor()\r\n   at
PickMeUp.RiderMainMenu.startsearch_SelectedIndexChanged(Object sender,
EventArgs e)\r\n   at
System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e)\r\n  
at System.Windows.Forms.ListView.UpdateSelectedIndex(Int32 iItem)\r\n  
at System.Windows.Forms.ListView.WnProc(WM wm, Int32 wParam, Int32
lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm,
Int32 wParam, Int32 lParam)\r\n   at
Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)\r\n   at
System.Windows.Forms.Form.ShowDialog()\r\n   at
PickMeUp.SelectMode.RiderMode_SelectedIndexChanged(Object sender,
EventArgs e)\r\n   at
System.Windows.Forms.ListView.OnSelectedIndexChanged(EventArgs e)\r\n  
at System.Windows.Forms.ListView.UpdateSelectedIndex(Int32 iItem)\r\n  
at System.Windows.Forms.ListView.WnProc(WM wm, Int32 wParam, Int32
lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm,
Int32 wParam, Int32 lParam)\r\n   at
Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)\r\n   at
System.Windows.Forms.Form.ShowDialog()\r\n   at
PickMeUp.FLogin.Login_Click(Object sender, EventArgs e)\r\n   at
System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at
System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at
System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32
lParam)\r\n   at System.Windows.Forms.Control._InternalWnProc(WM wm,
Int32 wParam, Int32 lParam)\r\n   at
Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n   at
System.Windows.Forms.Application.Run(Form fm)\r\n   at
PickMeUp.Program.Main()\r\n"    string

Ce qui est
bizarre c'est qu'un coup j'obtiens ça, un coup j'obtiens un
NullReferenceException mais avec un message comme quoi je devrais
utiliser :Invoke.

"Control.Invoke must be used to interact with controls created on a separate thread."    string
Je tiens à signaler que j'ai tenté d'utiliser un delegate de la manière suivante :

        private delegate void GetMapThreadDelegate(IAsyncResult state);
        private void DoUpdate2(IAsyncResult state)
        {

            if (this.InvokeRequired)
            {
    
                this.Invoke(new GetMapThreadDelegate(DoUpdate2),new object[] { state });
                return;
            }
        
            MapImage[] images = rss.EndGetMap(state);
            imageStream = new System.IO.MemoryStream(images[0].MimeData.Bits);

        }

Avec la mise à jour effectuée dans GetMapThread ( qui appelle la méthode DoUpdate2 )

J'ai
cherché sur le net de l'aide sur ce genre de msg d'erreur apparemment
c'est assez connu comme problème, mais je n'ai pas su adapter les
solutions trouvées à mon cas :(

Merci pour votre aide grandement appréciée !
0
Rejoignez-nous