.NET Threading Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arun Kumar

    #16
    Re: .NET Threading Question

    Listen Bro, I can guess you never used Delphi!!! If you don't like to
    comment then DON'T comment, Delphi or C# makes no diff... both rocks.

    its the .Net platform I am talking about.

    Be nice.

    ..Net rules when you use properly.




    "Delphi Hater" <Delphi Hater@discussio ns.microsoft.co m> wrote in message
    news:433ABBBE-F2FE-4F16-B95D-7E56E3D6DF63@mi crosoft.com...[color=blue]
    > Listen bra... just call Invoke(). if you don't like it, go back to wack
    > Delphi!
    >
    > .NET is ill!!!!
    >
    > "Arun Kumar" wrote:
    >[color=green]
    >> I know, its not true. Its sample app and was trying thing out.
    >>
    >> Thanks for your response.
    >>
    >> Arun
    >>
    >>
    >> "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
    >> news:MPG.1cca18 4c93c1fa5a98bfd e@msnews.micros oft.com...[color=darkred]
    >> > Arun Kumar <arunpv@hotmail .com> wrote:
    >> >> I agree, its not the best practices but my point is, if you use
    >> >> threads
    >> >> and
    >> >> use it carefully it shouldn't be a problem.
    >> >
    >> > Not true.
    >> >
    >> >> Check this link
    >> >> http://www.yoda.arachsys.com/csharp/...winforms.shtml
    >> >
    >> > Which part of:
    >> >
    >> > <quote>
    >> > Never invoke any method or property on a control created on another
    >> > thread other than Invoke, BeginInvoke, EndInvoke or CreateGraphics, and
    >> > InvokeRequired.
    >> > </quote>
    >> >
    >> > gives you the impression that it shouldn't be a problem?
    >> >
    >> > --
    >> > Jon Skeet - <skeet@pobox.co m>
    >> > http://www.pobox.com/~skeet
    >> > If replying to the group, please do not mail me too[/color]
    >>
    >>
    >>[/color][/color]


    Comment

    • Bob Powell [MVP]

      #17
      Re: .NET Threading Question

      Read up on the InvokeRequired property and the Invoke and BeginInvoke
      methods.

      Basically you should not directly acces the UI thread's objects from a non
      UI thread.

      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.





      "Arun Kumar" <arunpv@hotmail .com> wrote in message
      news:%23gsEZvdQ FHA.1176@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > What is wrong with this code. All i am trying to test is 3 progressbar and
      > one button. On buttonclick i create 3 threads and each thread calls a
      > method which in turn updates the progressbar and it works. I would to know
      > if this can be used.
      > Thanks
      > private void button1_Click(o bject sender, System.EventArg s e)
      >
      > {
      >
      > ThreadStart job = new ThreadStart(one method);
      >
      > Thread thread = new Thread(job);
      >
      > thread.Start();
      >
      > ThreadStart job1 = new ThreadStart(sec ondmethod);
      >
      > Thread thread1 = new Thread(job1);
      >
      > thread1.Start() ;
      >
      > ThreadStart job2 = new ThreadStart(thi rdmethod);
      >
      > Thread thread2 = new Thread(job2);
      >
      > thread2.Start() ;
      >
      >
      >
      > }
      >
      > public void onemethod()
      >
      > {
      >
      > for (int i=0;i<100;i++)
      >
      > {
      >
      > progressBar1.Va lue = i;
      >
      > Thread.Sleep(10 0);
      >
      > }
      >
      > }
      >
      > public void secondmethod()
      >
      > {
      >
      > for (int i=0;i<100;i++)
      >
      > {
      >
      > progressBar2.Va lue = i;
      >
      > Thread.Sleep(10 0);
      >
      > }
      >
      > }
      >
      > public void thirdmethod()
      >
      > {
      >
      > for (int i=0;i<100;i++)
      >
      > {
      >
      > progressBar3.Va lue = i;
      >
      > Thread.Sleep(10 0);
      >
      > }
      >
      > }
      >
      >[/color]


      Comment

      • Willy Denoyette [MVP]

        #18
        Re: .NET Threading Question


        "Arun Kumar" <arunpv@hotmail .com> wrote in message
        news:eyN1i0eQFH A.244@TK2MSFTNG P12.phx.gbl...[color=blue]
        >
        > "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
        > news:%230DZhdeQ FHA.2680@TK2MSF TNGP09.phx.gbl. ..[color=green]
        >>
        >> "Arun Kumar" <arunpv@hotmail .com> wrote in message
        >> news:OGvzEQeQFH A.3404@TK2MSFTN GP12.phx.gbl...[color=darkred]
        >>> DotNet Coder,
        >>>
        >>> I agree, its not the best practices but my point is, if you use threads
        >>> and use it carefully it shouldn't be a problem.
        >>> Check this link
        >>> http://www.yoda.arachsys.com/csharp/...winforms.shtml
        >>>
        >>> let me know you comments.
        >>>
        >>> Arun
        >>>[/color]
        >>
        >> There is nothing "carefully" with this, it may work in this simple sample
        >> and it may work a million times but sooner or later it will break, it's
        >> simply wrong to touch the UI element (a window handle) from a thread
        >> other than the thread that owns the handle.
        >>
        >> Willy.
        >>
        >>[/color]
        > Thanks for your response.
        >
        > Yes. I know that. there is nothing called "carefully" . I have worked on
        > Threads alot in Win32 and I know what will have happen.[color=green]
        >>" simply wrong to touch the UI element (a window handle) from a thread
        >>other than the thread that owns the handle."[/color]
        > True. its a sample, yes I am trying out things what i can and what i
        > cannot do in .Net and what are the limitations and I strong believe there
        > is nothing wrong in doing that and putting my words out here and get the
        > best out it.
        >
        > Arun
        >[/color]

        These, what you call "limitation s", are imposed by "Windows" (Win32), not by
        ..NET. "Window" handles (just like Win32 Mutexes) have thread affinity, and
        as such they shouldn't be used from another thread than the owning thread
        (the one that created the window). Every system or framework that runs on
        top of this has to repect this very rule, failing to do so will fail sooner
        or later.
        Note that v2.0 will throw an exception when running your sample in debug
        mode, or when the CheckForIllegal CrossThreadCall s property is set to true in
        release mode.

        Willy.





        Comment

        Working...