Click here to Skip to main content
15,905,785 members
Home / Discussions / C#
   

C#

 
GeneralRe: Install Automatic Updates C# 2008 .Net app Pin
PDTUM15-Feb-10 12:20
PDTUM15-Feb-10 12:20 
GeneralRe: Install Automatic Updates C# 2008 .Net app Pin
Not Active15-Feb-10 13:29
mentorNot Active15-Feb-10 13:29 
GeneralRe: Install Automatic Updates C# 2008 .Net app Pin
PDTUM15-Feb-10 13:41
PDTUM15-Feb-10 13:41 
GeneralRe: Install Automatic Updates C# 2008 .Net app Pin
Not Active15-Feb-10 14:23
mentorNot Active15-Feb-10 14:23 
AnswerSame answers to to the same question Pin
Not Active15-Feb-10 13:30
mentorNot Active15-Feb-10 13:30 
GeneralRe: Same answers to to the same question Pin
PDTUM15-Feb-10 14:00
PDTUM15-Feb-10 14:00 
GeneralRe: Same answers to to the same question [modified] Pin
Not Active15-Feb-10 14:20
mentorNot Active15-Feb-10 14:20 
Question[partially SOLVED] Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 5:46
User 680178015-Feb-10 5:46 
Hi everyone,
I have a small problem. I am trying to multi-thread my program, so the GUI will not freeze when the engine does certain calculations.
Class1:

  private delegate void DelegateToCrossThread_None(); 

MethodA:

  
this.thread_MainEngine = new Thread(new ThreadStart(this.Initiate_Engine));  

this.textBoxReport.Clear();
//thread_MainEngine.IsBackground = true;
this.thread_MainEngine.Start();
this.textBoxReport.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: INITIALIZATION\r\n";
 

MethodB:

 
	private void Options()
        {
            if (this.InvokeRequired)
            {
                DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(Options);
                this.Invoke(crossThreader);
            }
            else
            {                //log options
                if (this.cboxLog.Checked != true)
                {
                    logOption = false; //brief, performance OK

                }
                else
                    logOption = true; //detailed, performance BAD}
 

MethodC:

  
		if (this.InvokeRequired)
            {
                DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(SettingsInfoDisplay);
                this.Invoke(crossThreader);
            }
            else
            {
			//
			}
 

Major method:

  
		private void Initiate_Engine()
        {
            if (this.InvokeRequired)
            {
                DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(Initiate_Engine);
                this.Invoke(crossThreader);
            }
            else
            {                
                SettingsInfoDisplay(); //displaying information for visual guiding
                Options(); //options for training

                this.textBoxReport.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: INPUT [" + inN + "]\r\n";
				
				mainEngine_instance = new Main_Engine_Class();
				
				mainEngine_instance.LogReportSynch += new StringSynch(mainEngine_instance_LogReportSynch);
				
				mainEngine_instance.Training(inN, hN, oN, erT, tt, progressFilter, logOption);
				
				Thread.Abort();
 

Class2:

 public event StringSynch LogReportSynch;

 

 if (dynamicLogOption == false)
            {LogReportSynch = null;}
 

 
	stepByStep_reportString = "[" + DateTime.Now.TimeOfDay.ToString() + "]: text" + "\r\n";
            if (LogReportSynch != null)
            {
                LogReportSynch(stepByStep_reportString);
            } 

The problem is:
if logOption is set to false, the event does not occur and the program runs as if there is no multi-threading. If it is set to true and there is
Application.DoEvents()
in mainEngine_instance_LogReportSynch, the textbox adds new lines of text I want. Why is that? Is there a better solution? The thing is I have lots of methods and variables I need to cross-thread, so I have a lot of
if(InvokeRequired)
.
Also, I have other events that dynamically update certain controls, and if Application.DoEvents() is not included, the program runs as if it is on 1 thread (no constant "updates" in text boxes). Any help pls?

modified 1-Aug-19 21:02pm.

AnswerRe: Cross-Threading problem, InvokeRequired and Events Pin
Nicholas Butler15-Feb-10 8:01
sitebuilderNicholas Butler15-Feb-10 8:01 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 8:17
User 680178015-Feb-10 8:17 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Nicholas Butler15-Feb-10 8:37
sitebuilderNicholas Butler15-Feb-10 8:37 
JokeRe: Cross-Threading problem, InvokeRequired and Events Pin
Wes Aday15-Feb-10 9:25
professionalWes Aday15-Feb-10 9:25 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Wes Aday15-Feb-10 11:10
professionalWes Aday15-Feb-10 11:10 
AnswerRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 8:37
sitebuilderLuc Pattyn15-Feb-10 8:37 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 9:55
User 680178015-Feb-10 9:55 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 10:01
sitebuilderLuc Pattyn15-Feb-10 10:01 
QuestionRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 10:23
User 680178015-Feb-10 10:23 
AnswerRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 10:32
sitebuilderLuc Pattyn15-Feb-10 10:32 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 10:38
User 680178015-Feb-10 10:38 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 11:30
sitebuilderLuc Pattyn15-Feb-10 11:30 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 10:45
User 680178015-Feb-10 10:45 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 11:23
sitebuilderLuc Pattyn15-Feb-10 11:23 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 8:41
Daniel Grunwald16-Feb-10 8:41 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn16-Feb-10 9:06
sitebuilderLuc Pattyn16-Feb-10 9:06 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 9:26
Daniel Grunwald16-Feb-10 9:26 

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.