• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

WPF Dispatcher

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    WPF Dispatcher

    Got a problem with updating a WPF control from an event.

    I have a WPF single form GUI, and am creating a thread to do some work.
    The thread messages back to the GUI two events, succeeded and failed events. In these events I want to notch a progress bar on one each time.

    I cannot seem to access the progress bar as it tells me

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

    Apparently this is because WPF objects inherit from Dispatch oojamaflip. Pages and pages of stuff to read and I'm up against a deadline.

    Can someone post some code to update the progress bar from within the event handler, and I can read up tonight.

    Thanks in advance.

    void _emailTotalCountMessage(object sender, MailFolder.EmailTotalCountMessageEventArgs e)
    {
    try
    {
    //The line below which is commented out was failing
    //pbTotalProgress.Maximum = Convert.ToDouble(e.mailCount);

    //All this nonsense below should work according to what I read but doesn't.
    pbTotalProgress.Dispatcher.Invoke(
    System.Windows.Threading.DispatcherPriority.Normal , new Action(
    delegate()
    {
    pbTotalProgress.Maximum = Convert.ToDouble(e.mailCount);
    }
    ));
    }
    catch(System.Exception ex)
    {
    Trace.WriteLine(ex.ToString());
    }
    }
    Knock first as I might be balancing my chakras.

    #2
    You worry me you really do.

    BackgroundWorker is what you are looking for.

    http://elegantcode.com/2009/07/03/wp...ess-to-the-ui/

    Comment


      #3
      You can use timer to update GUI from variables set by worker threads that are not supposed to access GUI because Windows programmers were too tulipy to implement good thread safe APIs.

      Comment


        #4
        Ignore Atw, BackgroundWorker is designed to make this task a piece of piss.

        Any other "solution" is total bollocks and will cause you pain.

        Comment


          #5
          Originally posted by DimPrawn View Post
          You worry me you really do.

          BackgroundWorker is what you are looking for.

          http://elegantcode.com/2009/07/03/wp...ess-to-the-ui/
          Why? Were you born knowing everything about every technology, or did you ever get thrown in the deep end with a new fangled technology you knew nothing about?
          Knock first as I might be balancing my chakras.

          Comment


            #6
            Originally posted by suityou01 View Post
            Why? Were you born knowing everything about every technology, or did you ever get thrown in the deep end with a new fangled technology you knew nothing about?
            I worry about the sort of clients building systems using contractors that don't know what they are doing, when there are plenty of people who do know what they are doing that are not being used.

            If I were a client and the technology is WPF and threading, why not hire a contractor expert in WPF and threading and chop the ones that don't?

            Or would you hire a carpenter to service a Ferrari engine and hope he picks it up as he goes along?

            Comment


              #7
              Originally posted by DimPrawn View Post
              I worry about the sort of clients building systems using contractors that don't know what they are doing, when there are plenty of people who do know what they are doing that are not being used.

              If I were a client and the technology is WPF and threading, why not hire a contractor expert in WPF and threading and chop the ones that don't?

              Or would you hire a carpenter to service a Ferrari engine and hope he picks it up as he goes along?
              Quite cutting. I was hired for workflow and document management skills. There is a wpf project that needed converting to multi threading so as not to block the GUI. I got handed it, and the client knows I do not know WPF and is being patient. Sometimes you cannot hire someone who knows all of the technologies in depth, but has enough underpinning knowledge to get by, and is prepared to tighten up in the weaker areas in his/her own time.

              Or as a wise old owl said to me once, life's imperfect, get used to it. Can I suggest you climb down from your ivory tower?

              Tosser.
              Knock first as I might be balancing my chakras.

              Comment


                #8
                Oh, technical thread cat fight. This should be fun!!!

                I bet ten quatloos on the funny looking guy in a Prawn suit to come up with the best reposte!!!

                PS - I haven't done WinForms for years, so no Wiser-than-thou comments from me!!!

                Comment


                  #9
                  Can't be bothered.

                  Good luck with your WPF project.

                  Comment


                    #10
                    Originally posted by DimPrawn View Post
                    Ignore Atw, BackgroundWorker is designed to make this task a piece of piss.
                    Can BackgroundWorker update any or all GUI elements from within its own thread? It seems that it can report progress via callbacks, but can it change other elements like text on GUI?

                    Comment

                    Working...
                    X