Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. cross thread operations

cross thread operations

Scheduled Pinned Locked Moved C#
tutorialannouncementcareer
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spiritboy
    wrote on last edited by
    #1

    look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).

    L D 2 Replies Last reply
    0
    • S spiritboy

      look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      S 2 Replies Last reply
      0
      • S spiritboy

        look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Have you tried progressbar.Invoke?

        1 Reply Last reply
        0
        • D DaveyM69

          Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          S Offline
          S Offline
          spiritboy
          wrote on last edited by
          #4

          I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Windows.Forms;
          namespace Server
          {
          static class Program
          {
          static System.Threading.Thread thread2 = null;
          [STAThread]
          static void Main()
          {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
          Form1 f = new Form1();
          thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
          thread2.IsBackground = true;
          thread2.ApartmentState = System.Threading.ApartmentState.STA;
          thread2.Start();
          try
          {
          f.job1 += new EventHandler(Job1);
          f.job2 += new EventHandler(Job2;
          f.job3 += new EventHandler(Job3);
          Application.Run(f);
          }
          catch(Exception r)
          {
          }
          }
          static void worker()
          {
          //create a splash screen and update its progress due to events job1,job2 and job3
          }
          }

          H S 2 Replies Last reply
          0
          • S spiritboy

            I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Windows.Forms;
            namespace Server
            {
            static class Program
            {
            static System.Threading.Thread thread2 = null;
            [STAThread]
            static void Main()
            {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Form1 f = new Form1();
            thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
            thread2.IsBackground = true;
            thread2.ApartmentState = System.Threading.ApartmentState.STA;
            thread2.Start();
            try
            {
            f.job1 += new EventHandler(Job1);
            f.job2 += new EventHandler(Job2;
            f.job3 += new EventHandler(Job3);
            Application.Run(f);
            }
            catch(Exception r)
            {
            }
            }
            static void worker()
            {
            //create a splash screen and update its progress due to events job1,job2 and job3
            }
            }

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            Someone has asked a similar question to yours, here[^], perhaps you can get some ideas from there. There are also several articles here on CodeProject. Use the Search articles box, at the top of the page, and search for threaded splash screen, or similar.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            1 Reply Last reply
            0
            • D DaveyM69

              Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              S Offline
              S Offline
              spiritboy
              wrote on last edited by
              #6

              In general speaking, i want to handle events that raise by thread1 and process them with thread2, that's all!

              L 1 Reply Last reply
              0
              • S spiritboy

                In general speaking, i want to handle events that raise by thread1 and process them with thread2, that's all!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                My first thought is a synchronized queue of delegates (which represent fired events) but that would be painful and probably overkill.. It's just a thought, not advice.

                1 Reply Last reply
                0
                • S spiritboy

                  I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Windows.Forms;
                  namespace Server
                  {
                  static class Program
                  {
                  static System.Threading.Thread thread2 = null;
                  [STAThread]
                  static void Main()
                  {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                  Form1 f = new Form1();
                  thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
                  thread2.IsBackground = true;
                  thread2.ApartmentState = System.Threading.ApartmentState.STA;
                  thread2.Start();
                  try
                  {
                  f.job1 += new EventHandler(Job1);
                  f.job2 += new EventHandler(Job2;
                  f.job3 += new EventHandler(Job3);
                  Application.Run(f);
                  }
                  catch(Exception r)
                  {
                  }
                  }
                  static void worker()
                  {
                  //create a splash screen and update its progress due to events job1,job2 and job3
                  }
                  }

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  Does the worker() method create a Form? What do the event handlers for Job1, Job2 and Job3 do - update the form created inside worker? If you're getting a CrossThread exception, it means you're updating *some* UI control from the wrong thread - it doesn't necessary have to be the main Form.

                  Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups