Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
QuestionError in C# Pin
tjpraveen238-May-06 0:11
tjpraveen238-May-06 0:11 
AnswerRe: Error in C# Pin
Christian Graus8-May-06 0:35
protectorChristian Graus8-May-06 0:35 
GeneralRe: Error in C# Pin
tjpraveen238-May-06 1:29
tjpraveen238-May-06 1:29 
GeneralRe: Error in C# Pin
Colin Angus Mackay8-May-06 2:03
Colin Angus Mackay8-May-06 2:03 
GeneralRe: Error in C# Pin
Christian Graus8-May-06 10:07
protectorChristian Graus8-May-06 10:07 
Questionwebcam over LAN Pin
_ReignFire_7-May-06 22:18
_ReignFire_7-May-06 22:18 
QuestionThreading Pin
BadKarma7-May-06 22:07
BadKarma7-May-06 22:07 
AnswerRe: Threading Pin
stbaker8-May-06 4:05
stbaker8-May-06 4:05 
Include System.Threading in your using list:
--------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Configuration.Install;
using System.Reflection;

Define your threading variables:
--------------------
private static System.Threading.Thread ReadFromDTThread;
private static System.Threading.Thread IsAliveThread;
private static System.Threading.Thread WriteToLX1Thread;
private static System.Threading.Thread IsActiveThread;

-----
and any global variables in your class:

-------
private System.ComponentModel.Container components = null;
private static System.Collections.Queue LX1Queue = new Queue();
private static System.Text.StringBuilder datatracSql = new System.Text.StringBuilder(5000);
private System.ComponentModel.Container components = null;
private static System.Collections.Queue LX1Queue = new Queue();
private static System.Text.StringBuilder datatracSql = new System.Text.StringBuilder(5000);
private volatile static int RestartCount = 0;
private volatile static bool InterfaceStop = true;
private volatile static bool InterfacePaused = true;
private volatile static bool Interface1Alive = false;
private volatile static bool WriteToLX1Alive = false;
---------------
Declaring boolean and integer variables volatile tells the compiler to make no assumptions about the state of variables. It makes simple operations on these variables safe for multithreading without the need for locking (atomic).

Write your methods that will be performed in your threads:
--------------
private static void WriteToLX1()
{
int ErrorCount = 0;
int qCount = 0;
if (generalSwitch.TraceInfo)
Trace.WriteLine("DT2LXInterface:WriteToLX1 has started.");

while (true)
{
//Set Alive flag so that IsAlive knows thread is running
WriteToLX1Alive = true;
Hashtable LoanData;
//Lock queue use SyncRoot to perform sync on objects that have this method
lock (LX1Queue.SyncRoot)
{
qCount = LX1Queue.Count;
}

if (qCount == 0)
{

//Flush the current sql statements--write all pending data to LX when queue goes empty
LXData.IAccess.ExecuteBatchAdHocFlush();

//End thread if there are no records in the queue and interface is stopping
if (InterfaceStop)
return;
Thread.Sleep(InterfaceCycleTime);
continue;
}

lock(LX1Queue.SyncRoot)
{
LoanData = (Hashtable)LX1Queue.Dequeue();
}

NBPropertyBag getInfo = new NBPropertyBag();
getInfo.Contents = LoanData;

string AcctNO = getInfo.ReadProperty("_accountnumber");
string Label = getInfo.ReadProperty("_label");
string FileID = getInfo.ReadProperty("_fileid");
if (generalSwitch.TraceVerbose)
{
Trace.IndentLevel =0;
Trace.WriteLine("");
Trace.WriteLine(System.DateTime.Now.ToString());
Trace.WriteLine("--------------- DT2LXInterface:WriteToLX1 Determine BLL Start ----------------");
Trace.Indent();
Trace.WriteLine("AcctNO/Label/FileID" + AcctNO + "/" + Label + "/" + FileID);
Trace.Unindent();
}
// Use label to determine which BLL to Call
try
{
switch (Label)
{
case "GenUpdate":
clsDt2LXGenBLL.HandleMessage(LoanData);
break;
case "UndUpdate":
clsDT2LXUndBLL.HandleMessage(LoanData);
break;
case "MktUpdate":
clsDT2LXMktBLL.HandleMessage(LoanData);
break;
case "FunUpdate":
clsDt2LXFunBLL.HandleMessage(LoanData);
break;
case "FeeUpdate":
clsDT2LXFeeBLL.HandleMessage(LoanData);
break;
case "ImpUpdate":
clsDT2LXImpBLL.HandleMessage(LoanData);
break;
case "ActUpdate":
clsDT2LXActBLL.HandleMessage(LoanData);
break;
case "MersUpdate":
case "MersMomUpdate":
clsDT2LXMersBLL.HandleMessage(LoanData);
break;
case "ELIUpdate":
clsDT2LXELIBLL.HandleMessage(LoanData);
break;
case "CorUpdate":
clsDT2LXBrokerBLL.HandleMessage(LoanData);
break;
};

if (generalSwitch.TraceVerbose)
{
Trace.IndentLevel = 0;
Trace.WriteLine("--------------- DT2LXInterface:WriteToLX1 Determine BLL Finished ----------------");
}

StagedLoansProcessed++;
//Data from datatrac was added without any issues
ErrorCount = 0;
if (DT2LXBaseBLL.status != 0)
{
//There were minor issues
LogUtil.AddToInfoRow(AcctNO, FileID, "1", "1" + Label, DT2LXBaseBLL.issue);
DT2LXBaseBLL.ClearIssue();
}
}

catch (Exception e1)
{
if (generalSwitch.TraceError)
{
Trace.IndentLevel = 0;
Trace.WriteLine("");
Trace.WriteLine(System.DateTime.Now.ToShortDateString());
Trace.WriteLine("-----------Error in WriteToLX1----------------");
Trace.Indent();
Trace.WriteLine(e1.Message);
Trace.WriteLine("AcctNO/Label/FileID" + AcctNO + "/" + Label + "/" + FileID);
Trace.IndentLevel =0;
Trace.WriteLine("-----------Error in WriteToLX1----------------");
}


if (e1.Message.IndexOf("ORA-01033") > -1 || //Oracle shutdown
e1.Message.IndexOf("ORA-12154") > -1 || //Bad TNS name
e1.Message.IndexOf("ORA-01017") > -1 || //Bad userID/password
e1.Message.IndexOf("ORA-06576") > -1) //stored procedure not found
{
//Can not recover from these errors; shutdown interface
EventLog.WriteEntry(MyServiceName, "Non-recoverable error--terminating WriteTOLX1: " + e1.Message ,
EventLogEntryType.Error);
WriteToLX1Alive = false;
return;
}

//Data from datatrac was not added to LX for some reason
//Try again
string secondTry = getInfo.ReadProperty("_secondtry", "F");
if (secondTry == "F")
{
getInfo.WriteProperty("_secondtry", "T");
LoanData = getInfo.Contents;
lock (LX1Queue.SyncRoot)
{
LX1Queue.Enqueue(LoanData);
}
}

if (ErrorCount > MaxErrorCount)
{
EventLog.WriteEntry(MyServiceName, "More than" + MaxErrorCount.ToString() + " errors in succession, terminating WriteToLX1 thread. Thread will be restarted.", EventLogEntryType.Warning);
WriteToLX1Alive = false;
return;
}
}
}
}


------

Then you can start the thread in the main class by calling:

--------
//The method on this thread will try to Send loan to LX
//If it fails to import the loan it will update the try count by one
WriteToLX1Thread = new Thread(new ThreadStart(WriteToLX1));
WriteToLX1Thread.Name = "WriteToLX1";
WriteToLX1Thread.Priority = ThreadPriority.AboveNormal;
WriteToLX1Thread.Start();
//Allow thread to execute on uniproccessor machine
Thread.Sleep(0);
-------

The Thread.Sleep(0) must be done after creating the thread to make it safe for uniproccessor machines. If you do not call this on a uniproccessor machine the child thread will never start.








QuestionHow to work with timer control in C# Pin
kalyanhere7-May-06 21:53
kalyanhere7-May-06 21:53 
AnswerRe: How to work with timer control in C# Pin
Christian Graus7-May-06 22:07
protectorChristian Graus7-May-06 22:07 
QuestionAbout C programming Pin
SYS867-May-06 21:50
SYS867-May-06 21:50 
AnswerRe: About C programming Pin
J4amieC7-May-06 21:56
J4amieC7-May-06 21:56 
AnswerRe: About C programming Pin
Christian Graus7-May-06 22:06
protectorChristian Graus7-May-06 22:06 
QuestionHow to change ToolBar BackColor Pin
Partha De7-May-06 21:46
Partha De7-May-06 21:46 
AnswerRe: How to change ToolBar BackColor Pin
KrIstOfK7-May-06 22:39
KrIstOfK7-May-06 22:39 
QuestionNeed an advice on creating a text editor Pin
Cristoff7-May-06 21:05
Cristoff7-May-06 21:05 
AnswerRe: Need an advice on creating a text editor Pin
J4amieC7-May-06 21:55
J4amieC7-May-06 21:55 
GeneralRe: Need an advice on creating a text editor Pin
Cristoff7-May-06 23:34
Cristoff7-May-06 23:34 
GeneralRe: Need an advice on creating a text editor Pin
J4amieC7-May-06 23:41
J4amieC7-May-06 23:41 
GeneralRe: Need an advice on creating a text editor Pin
Cristoff8-May-06 4:08
Cristoff8-May-06 4:08 
QuestionInserting data into SQL Server database Pin
cshivaprasad7-May-06 19:51
cshivaprasad7-May-06 19:51 
AnswerRe: Inserting data into SQL Server database Pin
freshonlineMax7-May-06 20:12
freshonlineMax7-May-06 20:12 
GeneralRe: Inserting data into SQL Server database Pin
cshivaprasad7-May-06 21:34
cshivaprasad7-May-06 21:34 
AnswerRe: Inserting data into SQL Server database Pin
kalyanhere8-May-06 18:36
kalyanhere8-May-06 18:36 
Questionsppech sdk5.1 Pin
TheEagle7-May-06 19:38
TheEagle7-May-06 19:38 

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.