9 Replies - 986 Views - Last Post: 06 October 2012 - 01:45 PM Rate Topic: -----

#1 xzenon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 10

cross thread call

Posted 06 October 2012 - 11:47 AM

Hi im just wondering if any one can help me do a cross thread call i got this this.radioButton1.Checked = true; code in a diffrent thread then where the radiobutton is how can i call it?
Is This A Good Question/Topic? 0
  • +

Replies To: cross thread call

#2 tlhIn`toq  Icon User is online

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

Reputation: 5529
  • View blog
  • Posts: 11,838
  • Joined: 02-June 10

Re: cross thread call

Posted 06 October 2012 - 12:05 PM

then you have a design flaw. threads should not try to change GUI items on other threads its not about how to do it, its about how to seperate the work.

you want one class only, to be responible for the gui.

How do I?

[*]Q: ...get Form/class/thread 'A' to make a change or talk to Form/class/thread 'B'

NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:
Was This Post Helpful? 0
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: cross thread call

Posted 06 October 2012 - 12:22 PM

Hey: so there's a method called Invoke for every WinForms component. It adds an event the message queue for the UI thread. That's what you want to do.

Something like this:

delegate void doSomethingDelegate();

void doSomething(){ 
// update textbox
}

//from the other thread
void otherThread()
{
    textBox1.Invoke( new doSomethingDelegate(doSomething) );
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: cross thread call

Posted 06 October 2012 - 12:51 PM

Also: I'm not disagreeing with tlhIn`toq here. The UI should be managed by a single thread. However, the UI thread shouldn't be handling all of the non-UI work a program does. I can think of reasonable scenarios where you would do something like this:

// launch a background thread to raytrace
// the NYC skyline
void render()
{
    Thread th = new Thread(doSomethingSlow);
    th.IsBackground = true;
    th.Start();
}

void doSomethingSlow()
{
    // really slow stuff
    // queue an event to update a picture box.
}



The doSomethingSlow method isn't a UI thread, but it needs to notify the UI when it is done its work--and that's all its doing. It might need to re-enable a disabled button, or let the user know that a large file is done loading and is ready to work with. Using the pattern I mentioned, it's sending a message to the appropriate HWND, which is then handled by the UI thread when it is reached in the queue.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

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

Reputation: 5529
  • View blog
  • Posts: 11,838
  • Joined: 02-June 10

Re: cross thread call

Posted 06 October 2012 - 12:58 PM

[*]Separating data from GUI - PLUS - serializing the data to XML
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: 5529
  • View blog
  • Posts: 11,838
  • Joined: 02-June 10

Re: cross thread call

Posted 06 October 2012 - 01:06 PM

View PostNathanMullenax, on 06 October 2012 - 01:51 PM, said:

The doSomethingSlow method isn't a UI thread, but it needs to notify the UI when it is done its work--and that's all its doing. It might need to re-enable a disabled button, or let the user know that a large file is done loading and is ready to work with. Using the pattern I mentioned, it's sending a message to the appropriate HWND, which is then handled by the UI thread when it is reached in the queue.


No. Period. That's not an acceptable way to do things in production level code, so don't get in these bad habits.

The thread doing other work ONLY does that other work. It can raise an event making a notification such as "50% complete" - but it is not the job of the slow thread to turn on buttons etc. Look at the Backgroundworker. It raises a progress report event.

The thread responsible for the GUI then does what it is supposed to do.

The anology I like to use is makeing an emergency phone call to 911.
The observer calls and says "There is a fire at 1313 Mockingbird lane"
The observer does NOT call and say "Send 3 pumper trucker, 2 ambulances, 2 supervisors, 57 fire fighters, 7 police car".

It is the job of the caller to report an event. It is the job of the subscriber (the 911 operator in this case) to respond per its policy/programming.
Was This Post Helpful? 0
  • +
  • -

#7 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: cross thread call

Posted 06 October 2012 - 01:11 PM

I have a feeling you didn't actually read my posts. The non-UI thread isn't updating the UI in the example I posted. It's notifying the UI thread that it needs to be updated. There's a distinct difference. All of the code that decides what happens in response to this event resides appropriately within the UI thread. Like I said, I don't think we have any disagreement here.
Was This Post Helpful? 0
  • +
  • -

#8 xzenon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 10

Re: cross thread call

Posted 06 October 2012 - 01:23 PM

View PostNathanMullenax, on 06 October 2012 - 12:51 PM, said:

Also: I'm not disagreeing with tlhIn`toq here. The UI should be managed by a single thread. However, the UI thread shouldn't be handling all of the non-UI work a program does. I can think of reasonable scenarios where you would do something like this:

// launch a background thread to raytrace
// the NYC skyline
void render()
{
    Thread th = new Thread(doSomethingSlow);
    th.IsBackground = true;
    th.Start();
}

void doSomethingSlow()
{
    // really slow stuff
    // queue an event to update a picture box.
}



The doSomethingSlow method isn't a UI thread, but it needs to notify the UI when it is done its work--and that's all its doing. It might need to re-enable a disabled button, or let the user know that a large file is done loading and is ready to work with. Using the pattern I mentioned, it's sending a message to the appropriate HWND, which is then handled by the UI thread when it is reached in the queue.


Tnx man worked like a charm
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

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

Reputation: 5529
  • View blog
  • Posts: 11,838
  • Joined: 02-June 10

Re: cross thread call

Posted 06 October 2012 - 01:31 PM

View PostNathanMullenax, on 06 October 2012 - 02:11 PM, said:

I have a feeling you didn't actually read my posts. The non-UI thread isn't updating the UI in the example I posted. It's notifying the UI thread that it needs to be updated. There's a distinct difference. All of the code that decides what happens in response to this event resides appropriately within the UI thread. Like I said, I don't think we have any disagreement here.


I did read it. Where we differ is not on the specifics of how to update the gui, but on who is making the decision.

Quote

void doSomethingSlow()
11{
12    // really slow stuff
13    // queue an event to update a picture box.
14}



Line 13 should not queue an event to update a picturebox. It shouldn't even know the picture box exists. It should just make its own notification event

void doSomethingSlow()
11{
12    // really slow stuff
13    // Raise event saying "I'm done" - Subscriber can do whatever they want with that knowledge
14}

This post has been edited by tlhIn`toq: 06 October 2012 - 01:31 PM

Was This Post Helpful? 1
  • +
  • -

#10 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: cross thread call

Posted 06 October 2012 - 01:45 PM

Comprendo amigo. I see what you're saying. If I can try to sum it up here. You're talking about good OOP; I was simply talking about thread ownership.

To be sure, in the example I gave it would certainly be a lot cleaner to have something like a RayTracer class with a Done event that the UI can register a listener for--even if its exclusive purpose is to deliver an Image that the UI subsequently displays. Agreed.

Sorry for any misunderstanding/getting defensive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1