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
  1. Home
  2. General Programming
  3. C#
  4. threading with events

threading with events

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpcomdesign
4 Posts 3 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.
  • A Offline
    A Offline
    asugix
    wrote on last edited by
    #1

    this question especially for Sacha Barber because I learn threading from his article (Beginners Guide To Threading In .NET Part 2 of n). First Question : how to end / exit thread? From tutorial and MSDN help, I assume that thread is end when the thread function ends. Is this true ? Before 2nd question, this is my code on worker thread:

    public event ReportConversion ReportConversionDone;
    ...
    private void RutinKonversi(object fListobj)
    {
    ...
    OnStatusReport(pd); //raising event
    }
    protected void OnStatusReport(ProggressDoneEventArgs e)
    {
    if (ReportConversionDone != null)
    {
    ReportConversionDone(this, e);
    }
    }

    This is my code on UI thread for handling thread event:

    void conv_ReportConversionDone(object sender, ProggressDoneEventArgs e)
    {
    //marshal to UI thread
    if (sender == conv)
    {
    context.Post(new SendOrPostCallback(delegate(object gakPenting)
    {//inline code or anonymous method
    #region code event handler cross thread
    switch (e.ItemToReport)
    {
    ...
    }
    #endregion
    }), null);
    }
    }

    When I try passing data from background thread to UI, By putting breakpoints, I know if the thread is executed once. When I start worker thread for the first time (when application just started), event handler on UI is executed once. But when I start for third times, event handler is executed three times too. My 2nd question is : how to make event handler just executed once whenever worker thread is done. This is the screenshot : first time[^] third time I pressed extract[^]

    No one can prevent me to learn something

    C N 2 Replies Last reply
    0
    • A asugix

      this question especially for Sacha Barber because I learn threading from his article (Beginners Guide To Threading In .NET Part 2 of n). First Question : how to end / exit thread? From tutorial and MSDN help, I assume that thread is end when the thread function ends. Is this true ? Before 2nd question, this is my code on worker thread:

      public event ReportConversion ReportConversionDone;
      ...
      private void RutinKonversi(object fListobj)
      {
      ...
      OnStatusReport(pd); //raising event
      }
      protected void OnStatusReport(ProggressDoneEventArgs e)
      {
      if (ReportConversionDone != null)
      {
      ReportConversionDone(this, e);
      }
      }

      This is my code on UI thread for handling thread event:

      void conv_ReportConversionDone(object sender, ProggressDoneEventArgs e)
      {
      //marshal to UI thread
      if (sender == conv)
      {
      context.Post(new SendOrPostCallback(delegate(object gakPenting)
      {//inline code or anonymous method
      #region code event handler cross thread
      switch (e.ItemToReport)
      {
      ...
      }
      #endregion
      }), null);
      }
      }

      When I try passing data from background thread to UI, By putting breakpoints, I know if the thread is executed once. When I start worker thread for the first time (when application just started), event handler on UI is executed once. But when I start for third times, event handler is executed three times too. My 2nd question is : how to make event handler just executed once whenever worker thread is done. This is the screenshot : first time[^] third time I pressed extract[^]

      No one can prevent me to learn something

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Sounds like the code to start the thread is broken. And if you have a question for an article author, ask in the forum under the article.

      Christian Graus Driven to the arms of OSX by Vista.

      A 1 Reply Last reply
      0
      • A asugix

        this question especially for Sacha Barber because I learn threading from his article (Beginners Guide To Threading In .NET Part 2 of n). First Question : how to end / exit thread? From tutorial and MSDN help, I assume that thread is end when the thread function ends. Is this true ? Before 2nd question, this is my code on worker thread:

        public event ReportConversion ReportConversionDone;
        ...
        private void RutinKonversi(object fListobj)
        {
        ...
        OnStatusReport(pd); //raising event
        }
        protected void OnStatusReport(ProggressDoneEventArgs e)
        {
        if (ReportConversionDone != null)
        {
        ReportConversionDone(this, e);
        }
        }

        This is my code on UI thread for handling thread event:

        void conv_ReportConversionDone(object sender, ProggressDoneEventArgs e)
        {
        //marshal to UI thread
        if (sender == conv)
        {
        context.Post(new SendOrPostCallback(delegate(object gakPenting)
        {//inline code or anonymous method
        #region code event handler cross thread
        switch (e.ItemToReport)
        {
        ...
        }
        #endregion
        }), null);
        }
        }

        When I try passing data from background thread to UI, By putting breakpoints, I know if the thread is executed once. When I start worker thread for the first time (when application just started), event handler on UI is executed once. But when I start for third times, event handler is executed three times too. My 2nd question is : how to make event handler just executed once whenever worker thread is done. This is the screenshot : first time[^] third time I pressed extract[^]

        No one can prevent me to learn something

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        asugix wrote:

        this question especially for Sacha Barber because I learn threading from his article (Beginners Guide To Threading In .NET Part 2 of n).

        Then this is the wrong place. Article has a discussion board and author will get a notification when new messages are posted.

        asugix wrote:

        First Question : how to end / exit thread? From tutorial and MSDN help, I assume that thread is end when the thread function ends. Is this true ?

        YES.

        asugix wrote:

        how to make event handler just executed once whenever worker thread is done. This is the screenshot :

        You are not showing enough code to answer this.

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • C Christian Graus

          Sounds like the code to start the thread is broken. And if you have a question for an article author, ask in the forum under the article.

          Christian Graus Driven to the arms of OSX by Vista.

          A Offline
          A Offline
          asugix
          wrote on last edited by
          #4

          When I found breakpoint and run step by step, I know if my thread is running. Thank's, I move the question in the forum below article

          No one can prevent me to learn something

          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