Hellow any one help me i am suffering to get update progress while ingest asset through wpf get error The calling thread cannot access this object because a different thread owns it

Respondido Hellow any one help me i am suffering to get update progress while ingest asset through wpf get error The calling thread cannot access this object because a different thread owns it

  • Saturday, August 25, 2012 4:12 PM
     
      Has Code
      void Assets_OnUploadProgress(object sender, UploadProgressEventArgs e)
             {
    
                
                     // Checking if this thread has access to the object.
                 if (listBox1.Dispatcher.CheckAccess())
                     {
                         // This thread has access so it can update the UI thread.      
                         listBox1.Items.Add(e.Progress.ToString());
                     }
                     else
                     {
                         // This thread does not have access to the UI thread.
                         // Place the update method on the Dispatcher of the UI thread.
                         listBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                             (Action)(() => { e.Progress.ToString(); }));
                     }
                
               
               
             }
    //

    V.karthik

All Replies

  • Saturday, August 25, 2012 4:28 PM
     
     

    Hi,

    It seems that the BeginInvoke does not add anything to listBox1.Items.

    Your Invoked method and the direct UI update should do the same thing, I believe.

    Sandor


    Sandor


    • Edited by SandorN Saturday, August 25, 2012 4:28 PM typo
    •  
  • Saturday, August 25, 2012 4:48 PM
     
     

    Please show me some code with example


    V.karthik

  • Saturday, August 25, 2012 6:34 PM
     
      Has Code

    Please replace your line: 

    (Action)(() => { e.Progress.ToString(); } ));

    with this code: 

    (Action)(() => listBox1.Items.Add(e.Progress.ToString())));

    If this doesn't help, then please upload a project with the issue present.

    It would be good to see when, and how you are creating threads.


    Sandor

  • Sunday, August 26, 2012 4:28 AM
     
      Has Code

    i changed the line with what you shown in post but no use same error

    The calling thread cannot access this object because a different thread owns it

    (Action)(() => listBox1.Items.Add(e.Progress.ToString())));

    if you need code please refer this link click this

    in this link you see onUploadProgress Event this is working fine because it is console project.I convert this to wpf then the problem started

    static void OnUploadProgress(object sender, UploadProgressEventArgs e)
    {
        Console.WriteLine("Current file: " + e.CurrentFile);
        Console.WriteLine("    Bytes sent: " + e.BytesSent);
        Console.WriteLine("    Progress:  " + e.Progress);
    }
     
    
    


    V.karthik

  • Sunday, August 26, 2012 8:36 AM
     
     

    Hi Karthik,

    I looked at the link but it shows only partial code. 

    I would like to see how threads are created. Instead of me trying to recreate the fault, could you send me a project to fix?

    Thanks,

    Sandor


    Sandor

  • Sunday, August 26, 2012 2:32 PM
     
     

    http://www.mediafire.com/file/fgcu2pk22l2d2dd/Sailfish_Desktop_Application.rar

    download code from this link in this first click Connect button which is in left side top corner

    later you can  understand every thing my code job is take video file path from system and

    upload to azure cloud it retuns update progress here is the error

    as soon as possible please give me solution because my projects strucks here


    V.karthik


  • Sunday, August 26, 2012 9:37 PM
     
      Has Code

    Hi Karthik,

    Thanks for the project I am looking at it. I have a better idea of what is happening. 

    I have some missing dependencies in MainWindow.GetContext() so I am not able to debug it:

    Could not load file or assembly 'Microsoft.Data.Services.Client, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

    I will have more time tomorrow, I will fix dependency and then will try to fix it for you. Until that, please modify it as below and check if this helps:

             void Assets_OnUploadProgress(object sender, UploadProgressEventArgs e)
             {
                 UpdateListBox1(e);
             }
    
            private void UpdateListBox1(UploadProgressEventArgs e)
            {
                if (listBox1.Dispatcher.CheckAccess())
                {
                    // This thread has access so it can update the UI thread.      
                    listBox1.Items.Add(e.Progress.ToString());
                }
                else
                {
                    // This thread does not have access to the UI thread.
                    // Place the update method on the Dispatcher of the UI thread.
    
                    listBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateListBox1Del(UpdateListBox1), e);
    
                }
            }
    
            private delegate void UpdateListBox1Del(UploadProgressEventArgs e);


    Sandor

  • Monday, August 27, 2012 11:19 AM
     
     

    sorry to say this i tried the above code but i get the same error

    please install  windows azure media services SDK

    click this link

    https://www.windowsazure.com/en-us/develop/net/how-to-guides/media-services/#connect

    first install this so its working

    Windows Azure Media Services SDK for .NET

    if you get the same error

    Second install this  also

    WCF Data Services 5.0 for OData v3

    because sdk uses  wcf dataservices

    so run project it works









  • Monday, August 27, 2012 11:34 AM
     
     

    Hi Karthik,

    I'll look at it now. Thanks for info!

    While I am setting up the environment, please try calling listBox1.Dispatcher.Invoke(.....) 

    instead of:  listBox1.Dispatcher.BeginInvoke(.....) 

    As far as I remember every BeginInvoke call should have a corresponding EndInvoke call as well. That could cause the failure when the update event happens for the second time or more.

    Hopefully I can recreate it soon and that will help me to solve it.

    Thanks!



    • Edited by SandorN Monday, August 27, 2012 11:35 AM
    • Edited by SandorN Monday, August 27, 2012 11:40 AM
    •  
  • Monday, August 27, 2012 11:42 AM
     
     
     thank you for your response just now i changed but get the same error

    V.karthik

  • Monday, August 27, 2012 2:24 PM
     
     Answered Has Code

    Hi Karthik,

    I have managed to perform two upload with no error and the progress displayed in listBox1.

    The issue is that the status updates happen at the end of all Assets_OnUploadProgress events have been raised. This event comes from various threads and that is causing problems.

    I added this debug code to the event handler:

      Debug.WriteLine(String.Format("Assets_OnUploadProgress ManagedThreadId {0}"Thread.CurrentThread.ManagedThreadId));
      Debug.WriteLine("listBox1.Dispatcher.CheckAccess {0}: ", listBox1.Dispatcher.CheckAccess());
    

    Need to figure out how to get the updates done as they are reported in the upload progress event argument.

    Here is the second successful upload:

    Upload success


    DispatcherPriority.Send  invoking priority might be enough to solve the late progress display issue.

    Sandor




    • Edited by SandorN Monday, August 27, 2012 2:27 PM
    • Edited by SandorN Monday, August 27, 2012 2:35 PM
    • Edited by SandorN Monday, August 27, 2012 2:35 PM
    • Marked As Answer by karthik_in_msdn Monday, August 27, 2012 3:07 PM
    •  
  • Monday, August 27, 2012 3:11 PM
     
     

    O Great .........................

    Thank You Very much my Problem is Solved you did very good job


    V.karthik

  • Monday, August 27, 2012 3:29 PM
     
     

    hello

    i have small doubt where is e.progress printed


    V.karthik


  • Monday, August 27, 2012 3:33 PM
     
     
    can you show me detailed code

    V.karthik

  • Monday, August 27, 2012 5:26 PM
     
     

    Hi Karthik,

    The listbox status update is displayed in the listbox only when the progress reaches 100%. So we can't see a continuos progress feedback on the UI. That is not good. So I think it is not ready yet. But I can't spend more time on this today. Sorry.

    I need to clean the code up because I have done loads of different tests to get a continuos progress display on the UI.


    Sandor

  • Monday, August 27, 2012 6:07 PM
     
     

    sorry i miss understand with out reading full post just see the image of Wpf Window i conclude work is completed, no problem do this very leaser because you are just helping me it is no your problem it is my problem.  Thanks for spending time for this issue in India time is 12.00 pm so i go to sleep


    V.karthik


  • Tuesday, August 28, 2012 4:48 AM
     
     

    Hi Sandor N

    this may be help full to your research in wpf please read this

    5 days back in Windows Azure Media Services Forms i posted the same problem then

    somebody done the job using  Coworker(click to refer)  and upload working fine but in

    windows form then i conver it to wpf but not working  windows code link for reference

    http://www.mediafire.com/file/nkblad4t0nx6wiz/coworker_modified.rar


    V.karthik


  • Tuesday, August 28, 2012 5:02 AM
     
     

     Hi,Sandor

    i give you some information it is helpful for your research

    5 days  back i posted the same error in Windows Azure Media Services Forms

    somebody answered they did upload using Coworker (click to refer) in windows forms

    here is the code link  http://www.mediafire.com/?nkblad4t0nx6wiz

    then i convert this in  to WPF Application it is not working and give same error

    once you refer this it may be helpful to you. Thank you


    V.karthik

  • Tuesday, August 28, 2012 6:37 PM
     
     

    Hi Karthik,

    I will have a look. Thanks!

    I would like to solve your problem because it is a very interesting one. Have a look at MVVM UI design pattern because that is very good to use with WPF.

    Hopefully you have a bit more patience because I am quite busy now. I will have time only during the weekend.

    Will come back to you soon!



    Sandor

  • Wednesday, August 29, 2012 4:47 AM
     
     
    ok thankyou

    V.karthik

  • Saturday, September 01, 2012 6:43 PM
     
     Answered Has Code

    Hi Karthik,

    I got it working now. I can email you the project, but for now here are the changes I made:

    1)  Add a sync object to the MainWindow class

        public partial class MainWindow : Window
        {
            // Add a sync object
            private readonly object _syncObject = new object();
    
            // Rest of the code not shown
            ....
            ....
        }

    2) Replace the synchronous call with the Async version in "btnupload_Click"

    // IAsset theAsset = _context.Assets.Create(local);
    Task<IAsset> theAsset = _context.Assets.CreateAsync(new string[]{local}, local, AssetCreationOptions.None, new CancellationToken());
    

    3) Finally use the syncObject in the progress event handler

            void Assets_OnUploadProgress(object sender, UploadProgressEventArgs e)
            {
                lock (_syncObject)
                {
                    if (listBox1.Dispatcher.CheckAccess())
                    {
                        // This thread has access so it can update the UI thread.      
                        listBox1.Items.Add(e.Progress.ToString());
                    }
                    else
                    {
                        // This thread does not have access to the UI thread.
                        // Place the update method on the Dispatcher of the UI thread.
                        listBox1.Dispatcher.Invoke(DispatcherPriority.Normal,
                            (Action)(() => listBox1.Items.Add(e.Progress.ToString())));
                    }
                }
            }
    

    This works fine for me. I get continuous progress updates on the UI. (listbox1)



    Sandor

    • Marked As Answer by karthik_in_msdn Sunday, September 02, 2012 11:09 AM
    •  
  • Sunday, September 02, 2012 7:27 AM
     
     

    OK Thank you please send  code to sailfishvideos@gmail.com and i am very thank full to you


    V.karthik

  • Sunday, September 02, 2012 11:11 AM
     
     
    Great Programmer

    V.karthik

  • Sunday, September 02, 2012 11:40 AM
     
     

    Hi Karthik,

    Thanks very much!

    As I wrote in my email, I will create a version of this project which uses the MVVM UI design pattern.

    It is highly recommended to use UI design patterns when building business logic to drive a user interface.



    Sandor