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. problem accessing a control from a thread (the callback is in another user defined class)

problem accessing a control from a thread (the callback is in another user defined class)

Scheduled Pinned Locked Moved C#
debuggingxmlhelpquestion
4 Posts 2 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.
  • G Offline
    G Offline
    George Nistor
    wrote on last edited by
    #1

    Hi, I have a custom class CReadCDD where I intend to do some processing. I placed the callbacks also inside this class (like mLoadXmlOnHeap_WorkThread). From the Form class I instantiate my CReadCDD class and create the thread with call to mLoadXmlOnHeap_WorkThread. In mLoadXmlOnHeap_WorkThread (not to have an additional proprty for the Form, I send an event to the Form class with the text I want as event args); the event is received in the Forms class, it goes to mfHandleCustomEvent_PrintRichTextConsole but here after entering in Invoke method IT BLOCK my Form. The richtext control is not updated. Where I did the mistake? I read the MSDN but still I have problem to access the control from my thread :) George

    public class CReadCDD
    {
    	//Fields
    	private CProcessXMLdoc mdoc;
    	private string mFileName;
    	public  RichTextBox pRichTextBox1 {get; set;}
    	
    	//private Form1 myForm;
    
    	// Declare the event using EventHandler<T>
        public event EventHandler<CCustomEventArgs> RaiseCustomEvent;
    
        protected void pOnRaiseCustomEvent(CCustomEventArgs pEv)
        {
        	EventHandler<CCustomEventArgs> handler = RaiseCustomEvent;
    		
        	// Event will be null if there are no subscribers
            if (handler != null)
                handler(this, pEv);
        }
    
    	
        public CReadCDD(string pFileName, Form1 pForm)
    	{
       	//mDelSetText= mSetText;
       	
    	mFileName= 	pFileName;   	
    	//myForm= pForm;
    	}
    
    	    	
        #region Worker Threads
        public void mLoadXmlOnHeap\_WorkThread()
        {
        	mdoc = new CProcessXMLdoc(mFileName);
        	
        	CCustomEventArgs evObj= new CCustomEventArgs("Load The XML Data from Cdd file on the HEAP\\n");
        	
        	pOnRaiseCustomEvent(evObj);
        }
    	
    	// ......................................
    }
    

    namespace CddToCaplDiagnosticTestsGenerator
    {
    public partial class Form1 : Form
    {

        private CReadCDD objReadCdd;   
        public delegate void DelegateTypePrntText(string text);
        public DelegateTypePrntText mdelT;	
          	
    	public Form1()
        {
    		mdelT = new DelegateTypePrntText(mSetTextToRichTextControl);
    		InitializeComponent();
        }
    	
    	// Define what actions to take when the event is raised.
    	void mfHandleCustomEvent\_PrintRichTextConsole(object sender, CCustomEventArgs e)
    	{
    		byte debug=0;
    		
    		string
    
    C 1 Reply Last reply
    0
    • G George Nistor

      Hi, I have a custom class CReadCDD where I intend to do some processing. I placed the callbacks also inside this class (like mLoadXmlOnHeap_WorkThread). From the Form class I instantiate my CReadCDD class and create the thread with call to mLoadXmlOnHeap_WorkThread. In mLoadXmlOnHeap_WorkThread (not to have an additional proprty for the Form, I send an event to the Form class with the text I want as event args); the event is received in the Forms class, it goes to mfHandleCustomEvent_PrintRichTextConsole but here after entering in Invoke method IT BLOCK my Form. The richtext control is not updated. Where I did the mistake? I read the MSDN but still I have problem to access the control from my thread :) George

      public class CReadCDD
      {
      	//Fields
      	private CProcessXMLdoc mdoc;
      	private string mFileName;
      	public  RichTextBox pRichTextBox1 {get; set;}
      	
      	//private Form1 myForm;
      
      	// Declare the event using EventHandler<T>
          public event EventHandler<CCustomEventArgs> RaiseCustomEvent;
      
          protected void pOnRaiseCustomEvent(CCustomEventArgs pEv)
          {
          	EventHandler<CCustomEventArgs> handler = RaiseCustomEvent;
      		
          	// Event will be null if there are no subscribers
              if (handler != null)
                  handler(this, pEv);
          }
      
      	
          public CReadCDD(string pFileName, Form1 pForm)
      	{
         	//mDelSetText= mSetText;
         	
      	mFileName= 	pFileName;   	
      	//myForm= pForm;
      	}
      
      	    	
          #region Worker Threads
          public void mLoadXmlOnHeap\_WorkThread()
          {
          	mdoc = new CProcessXMLdoc(mFileName);
          	
          	CCustomEventArgs evObj= new CCustomEventArgs("Load The XML Data from Cdd file on the HEAP\\n");
          	
          	pOnRaiseCustomEvent(evObj);
          }
      	
      	// ......................................
      }
      

      namespace CddToCaplDiagnosticTestsGenerator
      {
      public partial class Form1 : Form
      {

          private CReadCDD objReadCdd;   
          public delegate void DelegateTypePrntText(string text);
          public DelegateTypePrntText mdelT;	
            	
      	public Form1()
          {
      		mdelT = new DelegateTypePrntText(mSetTextToRichTextControl);
      		InitializeComponent();
          }
      	
      	// Define what actions to take when the event is raised.
      	void mfHandleCustomEvent\_PrintRichTextConsole(object sender, CCustomEventArgs e)
      	{
      		byte debug=0;
      		
      		string
      
      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      After the first look I would say thrdLoadXmlOnHeap.Join(); is your problem. After you started your thread from your main/guithread your main/guithread waits for the end of the thread you started. This blocks your ui. At first I would try just to comment this line out.

      Greetings Covean

      G 1 Reply Last reply
      0
      • C Covean

        After the first look I would say thrdLoadXmlOnHeap.Join(); is your problem. After you started your thread from your main/guithread your main/guithread waits for the end of the thread you started. This blocks your ui. At first I would try just to comment this line out.

        Greetings Covean

        G Offline
        G Offline
        George Nistor
        wrote on last edited by
        #3

        Yep... thx

        C 1 Reply Last reply
        0
        • G George Nistor

          Yep... thx

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #4

          You are welcome.

          Greetings Covean

          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