Click here to Skip to main content
15,896,912 members
Home / Discussions / C#
   

C#

 
AnswerRe: Exchange Web Services (EWS) Pin
Ravi Bhavnani25-Mar-10 7:06
professionalRavi Bhavnani25-Mar-10 7:06 
GeneralRe: Exchange Web Services (EWS) Pin
MWRivera29-Mar-10 5:09
MWRivera29-Mar-10 5:09 
GeneralRe: Exchange Web Services (EWS) Pin
Travis Citrine2-Jul-15 17:56
Travis Citrine2-Jul-15 17:56 
QuestionExceptions across invoke Pin
richasea25-Mar-10 4:23
richasea25-Mar-10 4:23 
AnswerRe: Exceptions across invoke Pin
ricmil4225-Mar-10 4:50
ricmil4225-Mar-10 4:50 
GeneralRe: Exceptions across invoke Pin
richasea25-Mar-10 5:40
richasea25-Mar-10 5:40 
GeneralRe: Exceptions across invoke Pin
Keith Barrow25-Mar-10 6:17
professionalKeith Barrow25-Mar-10 6:17 
Questionproblem accessing a control from a thread (the callback is in another user defined class) Pin
George Nistor25-Mar-10 2:57
George Nistor25-Mar-10 2:57 
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 Smile | :)

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 text = e.pTextBox;
    		// Check if this method is running on a different thread
    		// than the thread that created the control.
    		
    		if (this.richTextBox1.InvokeRequired)
    		{
    			// It's on a different thread, so use Invoke.
    			this.Invoke (mdelT, new object[] { text + " (Invoke)" });
    		}
    		else // It's on the same thread, no need for Invoke
    			richTextBox1.AppendText("No Invoke");
    	}
        
        void mSetTextToRichTextControl(string pText)
        {
        	richTextBox1.AppendText("Inner thread");
        }

    	   	    
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Cdd files (*.cdd)|*.cdd";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                string filename = openFileDialog1.FileName;

                try
                {
                	objReadCdd= new CReadCDD(filename, this);
                	
                	objReadCdd.RaiseCustomEvent += mfHandleCustomEvent_PrintRichTextConsole;
                	
                	#region LoadXMLinMemory
                	objReadCdd.pRichTextBox1= richTextBox1;
                    
					Thread thrdLoadXmlOnHeap = new Thread(objReadCdd.mLoadXmlOnHeap_WorkThread);
					thrdLoadXmlOnHeap.Start();
					
					//richTextBox1.AppendText("Load The XML Data from Cdd file on the HEAP\n");
					
					thrdLoadXmlOnHeap.Join();
					#endregion
					
					#region Load Data in the CData tree
				//	Thread thrdLoadOnCDataTree = new Thread(objReadCdd.mLoadOnCDataTree_WorkThread);
				//	thrdLoadOnCDataTree.Start();
					
				//	richTextBox1.AppendText("End loading XML data in CData Tree\n");
					#endregion
  

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }
        
    }
}
<

modified on Thursday, March 25, 2010 9:09 AM

AnswerRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
Covean25-Mar-10 3:33
Covean25-Mar-10 3:33 
GeneralRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
George Nistor25-Mar-10 3:45
George Nistor25-Mar-10 3:45 
GeneralRe: problem accessing a control from a thread (the callback is in another user defined class) Pin
Covean25-Mar-10 3:59
Covean25-Mar-10 3:59 
QuestionHow to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 1:42
igalep13225-Mar-10 1:42 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
Not Active25-Mar-10 1:46
mentorNot Active25-Mar-10 1:46 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 1:56
igalep13225-Mar-10 1:56 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 2:06
igalep13225-Mar-10 2:06 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
Keith Barrow25-Mar-10 2:02
professionalKeith Barrow25-Mar-10 2:02 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 2:07
igalep13225-Mar-10 2:07 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 5:04
igalep13225-Mar-10 5:04 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
Keith Barrow25-Mar-10 5:25
professionalKeith Barrow25-Mar-10 5:25 
GeneralRe: How to get values after dictionary sorting by values with linq Pin
igalep13225-Mar-10 14:27
igalep13225-Mar-10 14:27 
AnswerRe: How to get values after dictionary sorting by values with linq Pin
lukaszbl25-Mar-10 2:04
lukaszbl25-Mar-10 2:04 
QuestionDelegates - What is the use of it? Pin
Praveen Raghuvanshi25-Mar-10 1:19
professionalPraveen Raghuvanshi25-Mar-10 1:19 
AnswerRe: Delegates - What is the use of it? Pin
harold aptroot25-Mar-10 1:38
harold aptroot25-Mar-10 1:38 
AnswerRe: Delegates - What is the use of it? [modified] Pin
Keith Barrow25-Mar-10 1:39
professionalKeith Barrow25-Mar-10 1:39 
GeneralRe: Delegates - What is the use of it? Pin
Not Active25-Mar-10 1:44
mentorNot Active25-Mar-10 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.