Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I am newbie to WPf.trying to update richtextbox control using dispatcher,while execution the application gets hangup but not goes to notresponding stage.it's working for particular minutes say (5)after that it get's freezes and updation carried out frequently.It will updates after the execution..
my code below...
Provide your suggestions....

Function to displaying information in richtextbox control

public void DisplayParamValues(string field, string expected, string actual, RichTextBox rtx,string tolerance)
{
Thread.Sleep(100);

string actualData = null;
string refData = null;

//// removing start of text,end of text,data link escape characters for actual
actualData = this.RemoveAltCharacters(actual);

//// removing start of text,end of text,data link escape characters for reference
refData = this.RemoveAltCharacters(expected);

string res = null;

if (Prop.ATS_Prop.Platformlimitation.Equals("Y"))
{
string platform=Prop.ATS_Prop.Platform;
string[] split=platform.Split('_');
string exp = pltfrmLimit.GetPlatfromLimitaion(field,Prop.ATS_Prop.Sink,split[0]);
if (exp != null)
{
refData = exp;
}

}

//// Check the tolerance applicable
if (!tolerance.Equals("NA"))
{
//// Converting tolerance value to double
double incTole = Convert.ToDouble(tolerance);
double decTole=-1* Convert.ToDouble(tolerance);
double actualVal=0;
double refVal=0;

//// Converting to double for actual and reference value
if (actualData.Contains(" ") && refData.Contains(" "))
{
String[] actDataSplit = actualData.Split(' ');
string[] refDataSplit=refData.Split(' ');
actualVal =Convert.ToDouble(actDataSplit[0]);
refVal = Convert.ToDouble(refDataSplit[0]);
}
else
{
actualVal =Convert.ToDouble(actualData);
refVal = Convert.ToDouble(refData);
}
double actTole=actualVal-refVal;

//// validating the PASS or FAIL
if (actualVal == refVal || actTole <= incTole || actTole >= decTole)
{
res = "PASS";
}

else
{
res = "FAIL";
}

//// writing the result to the trace
if (System.IO.File.Exists(Prop.ATS_Prop.FilePath))
{
writer = File.AppendText(Prop.ATS_Prop.FilePath);
if (res.Equals("PASS"))
{
writer.WriteLine(field + ": PASS\n");
}
else
{
writer.WriteLine(field + ": FAIL");
writer.WriteLine(":\nReference : " + expected + " Actual : " + actual);
}

writer.WriteLine();
writer.Flush();
writer.Close();
}
//// Displays the result to control
rtx.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
(ThreadStart)delegate()
{
if (res.Equals("PASS"))
{
rtx.AppendText(field + ": PASS\n");
if (Prop.ATS_Prop.AnalyScroll)
{
rtx.ScrollToEnd();
}
}
else
{
rtx.AppendText(field + ":FAIL\n");
rtx.AppendText("Reference : " + expected + " Actual : " + actual + "\n");
if (Prop.ATS_Prop.AnalyScroll)
{
rtx.ScrollToEnd();
}
}
});
}
else
{
//// appending result to the trace file
if (System.IO.File.Exists(Prop.ATS_Prop.FilePath))
{
writer = File.AppendText(Prop.ATS_Prop.FilePath);
if (refData.Equals(actualData))
{
writer.WriteLine(field + ": PASS\n");
}
else
{
writer.WriteLine(field + ": FAIL");
writer.WriteLine(":\nReference : " + expected + " Actual : " + actual);
}

writer.WriteLine();
writer.Flush();
writer.Close();
}
//// Displays the result to control
rtx.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
(ThreadStart)delegate()
{
if (refData.Equals(actualData))
{
rtx.AppendText(field + ": PASS\n");
if (Prop.ATS_Prop.AnalyScroll)
{
rtx.ScrollToEnd();
}
}
else
{
rtx.AppendText(field + ":FAIL\n");
rtx.AppendText("Reference : " + expected + " Actual : " + actual + "\n");
if (Prop.ATS_Prop.AnalyScroll)
{
rtx.ScrollToEnd();
}
}
});
}

}

Prop.ATS_Prop :


///
/// Get and Sets properties for ATS
///

public static class ATS_Prop
{
///
/// Name of the application used
///

private static string application;

///
/// Name of the platform used
///

private static string platform;

///
/// Test cases table name
///

private static string testcases;

///
/// Expected table name
///

private static string expected;

///
/// Workflow table name
///

private static string workflow;

///
/// Name of the source used
///

private static string source;

///
/// Name of the sink used
///

private static string sink;

///
/// Custom Parameter selection
///

private static string customParam;

///
/// Thread state
///

private static bool threadstate;

///
/// Stress occurences
///

private static int occurences;

///
/// Stress test ids
///

private static string stressTestIds;

///
/// Directory structure for dut trace
///

private static string dutFilePath;

///
/// Run button control
///

private static Button runButton;

///
/// Stop button initialization
///

private static Button stopButton;

///
/// Custom run button initialization
///

private static Button customRunButton;

///
/// Grid initialization
///

private static Grid displayGrid;

///
/// Checkbox initialization
///

private static CheckBox stressChck;

///
/// platform revision information
///

private static string dutHeader;

///
/// Platform revision
///

private static string rev;

///
/// revision label initialization
///

private static Label revision;

///
/// Directory structure of actual result trace
///

private static string filePath;

///
/// Directory structure of stress/Custom execution trace
///

private static string dir;

///
/// List of test ids
///

private static ArrayList testIds = new ArrayList();

///
/// DUT directory
///

private static string dutDir;

///
/// Dut auto scroll
///

private static bool dutScroll;

///
/// Analyzer auto scroll
///

private static bool analyScroll;

///
/// Testplan version
///

private static double testplanVersion;

///
/// Platform limitation
///

private static string platformlimitation;

#region Platformlimitation
///
/// Gets or sets Platform limitation
///

public static string Platformlimitation
{
get { return ATS_Prop.platformlimitation; }
set { ATS_Prop.platformlimitation = value; }
}
#endregion

#region Testplan Version
///
/// Gets or sets Testplan version
///

public static double TestplanVersion
{
get { return ATS_Prop.testplanVersion; }
set { ATS_Prop.testplanVersion = value; }
}
#endregion

#region AnalyzerScroll
///
/// Gets or sets Analyzer scroll
///

public static bool AnalyScroll
{
get { return ATS_Prop.analyScroll; }
set { ATS_Prop.analyScroll = value; }
}
#endregion

#region DUTScroll
///
/// Gets or sets Dut scroll
///

public static bool DutScroll
{
get { return ATS_Prop.dutScroll; }
set { ATS_Prop.dutScroll = value; }
}
#endregion

#region PropertyCustomParam
///
/// Gets or sets Custom Parameter
///

public static string CustomParam
{
get { return ATS_Prop.customParam; }
set { ATS_Prop.customParam = value; }
}
#endregion

#region PropertyThreadstate
///
/// Gets or sets thread state
///

public static bool Threadstate
{
get { return ATS_Prop.threadstate; }
set { ATS_Prop.threadstate = value; }
}
#endregion

#region PropertyOccurances
///
/// Gets or sets Occurrences
///

public static int Occurrences
{
get { return ATS_Prop.occurences; }
set { ATS_Prop.occurences = value; }
}
#endregion

#region PropertyStresstestids
///
/// Gets or sets Stress test ids
///

public static string StressTestIds
{
get { return ATS_Prop.stressTestIds; }
set { ATS_Prop.stressTestIds = value; }
}
#endregion

#region PropertyDutFilePath
///
/// Gets or sets DutFile path
///

public static string DutFilePath
{
get { return ATS_Prop.dutFilePath; }
set { ATS_Prop.dutFilePath = value; }
}
#endregion

#region PropertyRunButton
///
/// Gets or sets Run button
///

public static Button RunButton
{
get { return ATS_Prop.runButton; }
set { ATS_Prop.runButton = value; }
}
#endregion

#region PropertyStopButton
///
/// Gets or sets Stop Button
///

public static Button StopButton
{
get { return ATS_Prop.stopButton; }
set { ATS_Prop.stopButton = value; }
}
#endregion

#region propertyCustomRunButton
///
/// Gets or sets custom run button
///

public static Button CustomRunButton
{
get { return ATS_Prop.customRunButton; }
set { ATS_Prop.customRunButton = value; }
}
#endregion

#region PropertyDisplayGrid
///
/// Gets or sets Display Grid
///

public static Grid DisplayGrid
{
get { return ATS_Prop.displayGrid; }
set { ATS_Prop.displayGrid = value; }
}
#endregion

#region propertystresscheckbox
///
/// Gets or sets stress checkbox
///

public static CheckBox StressChck
{
get { return ATS_Prop.stressChck; }
set { ATS_Prop.stressChck = value; }
}
#endregion

#region PropertyDutheader
///
/// Gets or sets dut header
///

public static string DutHeader
{
get { return ATS_Prop.dutHeader; }
set { ATS_Prop.dutHeader = value; }
}
#endregion

#region propertyRev
///
/// Gets or sets revision
///

public static string Rev
{
get { return ATS_Prop.rev; }
set { ATS_Prop.rev = value; }
}
#endregion

#region PropertyRevision
///
/// Gets or sets Revision
///

public static Label Revision
{
get { return ATS_Prop.revision; }
set { ATS_Prop.revision = value; }
}
#endregion

#region PropertyFilepath
///
/// Gets or sets file path
///

public static string FilePath
{
get { return ATS_Prop.filePath; }
set { ATS_Prop.filePath = value; }
}
#endregion

#region PropertyDut
///
/// Gets or sets DUT
///

public static string DutDir
{
get { return ATS_Prop.dutDir; }
set { ATS_Prop.dutDir = value; }
}
#endregion

#region PropertyDir
///
/// Gets or sets Dir
///

public static string Dir
{
get { return ATS_Prop.dir; }
set { ATS_Prop.dir = value; }
}
#endregion

#region Propertytestids
///
/// Gets or sets test ids
///

public static ArrayList TestIds
{
get { return ATS_Prop.testIds; }
set { ATS_Prop.testIds = value; }
}
#endregion

#region propertyApplication
///
/// Gets or sets Property for application
///

public static string Application
{
get { return application; }
set { application = value; }
}
#endregion

#region Propertyplatform
///
/// Gets or sets property for platform
///

public static string Platform
{
get { return platform; }
set { platform = value; }
}
#endregion

#region propertytestcases
///
/// Gets or sets property for Test cases
///

public static string Testcases
{
get { return testcases; }
set { testcases = value; }
}
#endregion

#region Propertyexpected
///
/// Gets or sets property for Expected
///

public static string Expected
{
get { return expected; }
set { expected = value; }
}
#endregion

#region Propertyworkflow
///
/// Gets or sets property for Workflow
///

public static string Workflow
{
get { return workflow; }
set { workflow = value; }
}
#endregion

#region Propertysource
///
/// Gets or sets Property for Source
///

public static string Source
{
get { return ATS_Prop.source; }
set { ATS_Prop.source = value; }
}
#endregion

#region Propertysink
///
/// Gets or sets property for sink
///

public static string Sink
{
get { return ATS_Prop.sink; }
set { ATS_Prop.sink = value; }
}
#endregion
}
Posted
Updated 30-May-13 19:06pm
v2
Comments
Sergey Alexandrovich Kryukov 30-May-13 12:03pm    
Dispatcher.Invoke and Dispatcher.BeginInvoke (why do you use BeginInvoke, not more usually used Invoke?) are designed to avoid hanging, not create it! Do you call your BeginInvoke from a separate, non-UI thread? If not, it makes no sense.
Why are you using (ThreadStart) in delegate? It's possible, but suggests you don't understand what you are doing. Normally, System.Action is used.
—SA
Sergey Alexandrovich Kryukov 30-May-13 12:09pm    
OK, the code is quite bad (especially comparison with hard-coded string and hard-coded texts in general), but there is no apparent problem here. It could be not just here, but somewhere else. We cannot see what are "Prop", "AST_Prop", "res", "rtx", where your BeginInvoke is called, etc. What if the invoked method recursively call itself, until some condition is met? You need to use the debugger and find out what's going on. Besides, pass "Prop", "res" and "rtx" as parameters of the delegate instance.

Another alternative: simplify the code sample to a very simple but complete one, still manifesting the problem. Post it all using "Improve question".

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900