14 Replies - 2470 Views - Last Post: 03 February 2011 - 11:47 AM Rate Topic: -----

#1 vsemwal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 11

How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 05:44 AM

Here Code how to access window main form control from child thread

Let we have label in main form and we want to update this label by next thread not by current thread

code here
declare delegate

delegate void Setlabel(string label);

call method

private void SetTextlabel(string label)
        {
            if (this.textBox1.InvokeRequired)
            {
                Setlabel d = new Setlabel(SetTextlabel);
                this.Invoke(d, new object[] { label });
            }
            else
            {
                this.lb_text.Text = label;
            }
        }


now pass test to label by calling
setTextlable("Vinay Bhaskar Semwal");

Vijay Bhaskar Semwal
Visiting faculty
Madhaya Pradesh Technical University

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 03 February 2011 - 05:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Avoid cross thread error during using access control of main th

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 05:47 AM

We have a tutorial if you'd bothered to search. You'd think someone on a university's FACULTY would know how to do this. Explains a lot I guess.
Was This Post Helpful? 1
  • +
  • -

#3 vsemwal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-January 11

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 05:49 AM

How to Update control of main window form in another thread

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;
using Utility;
using Common;


namespace TwoExe
{
public delegate Setlabel;
    public partial class bhaskar : Form
    {
               
        Marshal m;
        public string url = "tcp://localhost:4444/Marshal";
        string res;
        
        public bhaskar()
        {
            InitializeComponent(); 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
/* run another thread child thread 
            Thread serverRun = new Thread(new ThreadStart(server));
            serverRun.Start();
            //server();
            
        }
        public void server()
        {
/...using >net remoting
            TcpChannel channel = new TcpChannel(3331);
            if (ChannelServices.GetChannel(channel.ChannelName) == null)
                ChannelServices.RegisterChannel(channel);
             m = new Marshal();
            RemotingServices.Marshal(m, "Marshal");
            res = m.result();

//..now updating lable of main form
            SetTextlabel(res);
        }
        public void SetTextlabel(string label)
        {
            if (this.label1.InvokeRequired)
            {
                Setlabel d = new Setlabel(SetTextlabel);
                this.Invoke(d, new object[] { label });
            }
            else
            {
                this.label1.Text = label;
            }
        }
}



Vijay Bhaskar Semwal
B.tech.+M.tech.
Software Engineer
Visiting Faculty
Madhaya Pradesh Technical University
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 06:00 AM

Have a look at this code snippet also:

http://www.dreaminco...snippet3288.htm

This question has been asked at least 2 billion times across the Internet. Have a look around and see what you find :). Your on the right lines though...

This post has been edited by CodingSup3rnatur@l-360: 03 February 2011 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 06:18 AM

Change this..

this.Invoke(d, new object[] { label });



to this....

label1.BeginInvoke(d, new object[] { label });


Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 07:36 AM

View PostJackOfAllTrades, on 03 February 2011 - 05:47 AM, said:

.... You'd think someone on a university's FACULTY would know how to do this. Explains a lot I guess.

If I could +25 you for that I would
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4487
  • View blog
  • Posts: 7,815
  • Joined: 08-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:03 AM

I'm not sure these were questions. I think these were attempted tutorials.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:14 AM

View PostinsertAlias, on 03 February 2011 - 10:03 AM, said:

I'm not sure these were questions. I think these were attempted tutorials.


Really? You think this was meant to be a tutorial and not a cry for help? :baaa:
Given how many problems were fixed for the OP ... I just don't know what to think.
Let's see... we already have good tutorials for the topic. This 'tutorial' was riddled with errors. Wasn't in the right section. Didn't use code tags. Was barely in English. Didn't make it's purpose clear.

vsemwal: If this was meant to be a tutorial and not a question needing help...
  • Hold off on future tutorials until you get some more experience.
  • After that, check to see if there is even an need (Read as "See if there is already a tutorial for the topic")
  • Check that your code actually works.
  • Explain your intent a LOT better.

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:15 AM

You may be right, but without further input from the OP, how are we to know?

I can't even tell what this is supposed to do. Is it incomplete? Why are there unnecessary member variables? It has unknown usings. Marshal seems to be an abstract class, unless that's part of the ancillary assemblies that aren't included, which may be because System.Runtime.InteropServices is not part of the equation.

WTH?
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4487
  • View blog
  • Posts: 7,815
  • Joined: 08-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:31 AM

The reason I think it's an attempted tutorial is that there's never any attempt to actually ask a question. Just a statement, code, and signature. I never said it wasn't a really crappy attempt though.

@Eclipsed, why change Invoke to BeginInvoke? The only difference is sync vs. async. Neither should actually be an issue.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:41 AM

View PostinsertAlias, on 03 February 2011 - 10:31 AM, said:

The reason I think it's an attempted tutorial is that there's never any attempt to actually ask a question. Just a statement, code, and signature. I never said it wasn't a really crappy attempt though.

@Eclipsed, why change Invoke to BeginInvoke? The only difference is sync vs. async. Neither should actually be an issue.


The more I re-read it, the more I think you're right.

Quote

Here Code how to access window main form control from child thread

Let we have label in main form and we want to update this label by next thread not by current thread

code here
declare delegate

delegate void Setlabel(string label);

call method

now pass test to label by calling
setTextlable("Vinay Bhaskar Semwal");

It does sound like a set of steps to follow - in REALLY bad English
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 10:50 AM

View PostinsertAlias, on 03 February 2011 - 01:31 PM, said:

The reason I think it's an attempted tutorial is that there's never any attempt to actually ask a question. Just a statement, code, and signature. I never said it wasn't a really crappy attempt though.

@Eclipsed, why change Invoke to BeginInvoke? The only difference is sync vs. async. Neither should actually be an issue.


I actually ran into an issue using Invoke. My application was throwing an exception(can't remember what it was), and when I looked it up, it was directly connected to using Invoke. After moving it to BeginInvoke, I never had the issue again.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5515
  • View blog
  • Posts: 11,824
  • Joined: 02-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 11:00 AM

View Posteclipsed4utoo, on 03 February 2011 - 10:50 AM, said:

View PostinsertAlias, on 03 February 2011 - 01:31 PM, said:

The reason I think it's an attempted tutorial is that there's never any attempt to actually ask a question. Just a statement, code, and signature. I never said it wasn't a really crappy attempt though.

@Eclipsed, why change Invoke to BeginInvoke? The only difference is sync vs. async. Neither should actually be an issue.


I actually ran into an issue using Invoke. My application was throwing an exception(can't remember what it was), and when I looked it up, it was directly connected to using Invoke. After moving it to BeginInvoke, I never had the issue again.


Personally I don't like doing the Invoke thing at all. It still means that thread B has to know about the controls on form A and try to directly affect them. That's just not right. Thread B should raise an event saying "I've done x" and any subscribed threads or forms should do their own response internally. I just don't see why someone would tightly bind two threads/controls like that.
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 11:04 AM

View PosttlhIn, on 03 February 2011 - 02:00 PM, said:

View Posteclipsed4utoo, on 03 February 2011 - 10:50 AM, said:

View PostinsertAlias, on 03 February 2011 - 01:31 PM, said:

The reason I think it's an attempted tutorial is that there's never any attempt to actually ask a question. Just a statement, code, and signature. I never said it wasn't a really crappy attempt though.

@Eclipsed, why change Invoke to BeginInvoke? The only difference is sync vs. async. Neither should actually be an issue.


I actually ran into an issue using Invoke. My application was throwing an exception(can't remember what it was), and when I looked it up, it was directly connected to using Invoke. After moving it to BeginInvoke, I never had the issue again.


Personally I don't like doing the Invoke thing at all. It still means that thread B has to know about the controls on form A and try to directly affect them. That's just not right. Thread B should raise an event saying "I've done x" and any subscribed threads or forms should do their own response internally. I just don't see why someone would tightly bind two threads/controls like that.


And I would do that now, but this app was written over 3 years ago(VS2005 and .Net 2.0), so back then, I had just learned about threading(and knew nothing about events).
Was This Post Helpful? 1
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4487
  • View blog
  • Posts: 7,815
  • Joined: 08-June 10

Re: How to Avoid cross thread error during using access control of main th

Posted 03 February 2011 - 11:47 AM

View PosttlhIn, on 03 February 2011 - 12:00 PM, said:

Personally I don't like doing the Invoke thing at all. It still means that thread B has to know about the controls on form A and try to directly affect them. That's just not right. Thread B should raise an event saying "I've done x" and any subscribed threads or forms should do their own response internally. I just don't see why someone would tightly bind two threads/controls like that.


Well, for the most part, you're right. Except when the event comes from another thread, the call still has to be invoked on the main thread.

But the way to do it properly is not to have the side thread do the invoking, but to have the control do the invoke itself if required. So the outside event is raised, triggering the handler on the current form, but on a different thread. The control that is being updated checks to see if an invoke is required, and if so, invokes. Otherwise, it just sets its value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1