c#

Passing delegate method as Parameter to start it as an own thread

To achieve the following sequence:

//1. Code until this position

//2. Code to run in a new thread

//3. Code that runs after thread is started

Just do the following:

Use the class ParameterizedThreadStart and pass the "code snippet" as an Action as parameter.
An example looks like this:

Create two methods:

public void RunMethodOnThread(Action action)
{
Thread thread1 = new Thread(new ParameterizedThreadStart(ExecuteInThread));
thread1.Start(action);
}

private void ExecuteInThread(Object a)
{
Action action = a as Action;
action.Invoke();
}

After that you can use the following call in your code, by passing a so called delegate function as an Parameter:

//1. Code until this position

RunMethodOnThread(delegate
{
//2. Code to run in a new thread
});

//3. Code that runs after thread is started

I know the solution seems a little bit complex, but by going this way you can achieve a very good reuse of your code and it is really easy to use it, especially if you have a lot of small "code snippets" that should run in a new thread.

Taggings:

Making unit tests for .NET framework in Visual Studio 2008

Finding a tool for automated unit tests that works with the .NET Framework. The backend classes are written in C# and the IDE i am using is Microsoft Visual Studio 2008.

How to reset font size formatting in XML/InfoPath file with c#

First of all we have to load the complet XML structures (root of XML Shema) in a variable.
Then wo do a search for the "font" tag and "/font" tag and replace it with an empty string.


public void resetfontsize()
{
// XPath to tho root of the XML Shema
string xml_data = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:Befund",NamespaceManager).InnerXml;
// set the Index of the searcher = 1
int nextindex = 1;
//if the search index greater than 0 --> make the search
while (nextindex > 0)
{
//serach for open tag font
nextindex = xml_data.IndexOf(" 0)
{
//search for close tag
int nextklammer = xml_data.IndexOf(">", nextindex);
string replstring = xml_data.Substring(nextindex, nextklammer - nextindex + 1);
//replace the result with empty string
xml_data = xml_data.Replace(replstring, "");
}
}
xml_data = xml_data.Replace("", "");
this.MainDataSource.CreateNavigator().SelectSingleNode("/my:Befund", NamespaceManager).InnerXml = xml_data;
}

Taggings:

How to reset font size formatting in XML/InfoPath file with c#

Currently i am developing an medical software. Therefor i use InfoPath form template with code behind c# and MS SQL or Infomix databse was a very comfortable solution. As the complexety of the system grows i had to broaden my horizon and find some tricky solutions to achieve the surreal requirements of the client. If users change the standard size formation of an richtext field and you wish to reset the font size back to the standard formatation this method will help you. People always want everyhing flexibel, fast and automatically,... properties which are very hard to reach within a software. So someday i encountered a very hard problem which took me a long time to solve it (although, afterwards it wasn't so difficult). If someone has programmed a little bit with c# before and knows about XML formating, its not that hard to rest the font size of richtext fields. So a solution had to be found. After hours of internet surfing and testing many methods i found the best way to solve this problem.

How to import rows to a repeating table in InfoPath

First we start to prepare the form:

1) Create a new infopath form
2) Rename the top element to “Root”
3) Add a “Repeating table” element with three columns to the infopath form
4) Name the group element “Details”
5) Name the repeating table element “RepTable”
6) Type a name for each element (”FieldA”, “FieldB”, “FieldC”)
7) In the repeating table properties within the tab “Data” do not allow the user to insert and delete rows. You must uncheck the checkbox (default is checked). We do that because we want to add automatically rows to the repeating table.

We want to load data to the repeating table when you click a button. It is your choice to make an on load event or react on some input to fill the table

1) Add a button to the form
2) On right click go to “Button properties”
3) Write on the label “Load”
4) Write as ID “LoadRepData”
5) Click edit form code. When you do that InfoPath will automatically add the necessary reference to the code behind file to react on the button
6) click event.

At this point we can start to code the InfoPath form. If you close the editor you can reach it if you click on the “Tools” menu “Programming” -> “Microsoft Visual Tools for Applications”.

Bind the navigator to the elements. Iterate the loop and fill the fields

XPathNavigator xPathNavi = MainDataSource.CreateNavigator();
XPathNavigator details = xPathNavi.SelectSingleNode(”/my:Root/my:Details”, NamespaceManager);
XPathNavigator repTable = details.SelectSingleNode(”my:RepTable”, NamespaceManager);
XPathNavigator node;
int counter = 0;
// iterate loop to fill table
for (int i=0; i<1; i++)
{
node = repTable.Clone();
node.SelectSingleNode(”my:FieldA”, NamespaceManager).SetValue(”FieldA - Row ” + i.ToString());
node.SelectSingleNode(”my:FieldB”, NamespaceManager).SetValue(”FieldB - Row ” + i.ToString());
node.SelectSingleNode(”my:FieldC”, NamespaceManager).SetValue(”FieldC - Row ” + i.ToString());
details.AppendChild(node);
counter += 1;
}

Because that we clone the row we need to remove the last row:

// delete a specific range of nodes
XPathNodeIterator rows = details.Select(”my:RepTable”, NamespaceManager);
XPathNavigator first = details.SelectSingleNode(”my:RepTable[1]”, NamespaceManager);
XPathNavigator second = details.SelectSingleNode(”my:RepTable[” + Convert.ToString(rows.Count - counter) + “]”, NamespaceManager);
first.DeleteRange(second);

How to import rows to a repeating table in InfoPath

Currently i am developing an medical software. Therefor i use InfoPath form template with code behind c# and MS SQL or Infomix databse was a very comfortable solution. As the complexety of the system grows i had to broaden my horizon and find some tricky solutions to achieve the surreal requirements of the client. If you wish to import data from database (like MS SQL or Informix) to an InfoPath Form this method will help you to get the data in a repeating table. People always want everyhing flexibel, fast and automatically, properties which are very hard to reach within a software. So someday i encountered a very hard problem which took me a long time to solve it (although, afterwards it wasn't so difficult). If someone has programmed a little bit with c# before, its not that hard to get content of a database within c#, but its not so easy to process this data in a repeating table in InfoPath. So a solution had to be found to show the data from the database in InfoPath. After hours of internet surfing and testing many methods i found the best way to solve this problem.

Development of a network tool for a network administator

We decided to develop the program in Microsoft .Net C# 2003. The startup of the pc’s were realized by wake on lan. Wake on lan is a message format. It contains eight times the mac adress of the network interface from the pc which should be started. This message is then send via a udp broadcast to his network interface which has to provide a wake on lan service.
To track the status of the clients we used UDP. The clients had to send a specific package (which also contains specific information) every couple of minutes, so that the server new that the client is online. For sending commands to the client the server established a TCP connection to the clients. This was used for example for the remote monitoring. For handling the clients the server implements a class for accepting incoming connections and managed each connection in a single thread. One challenge was to create no race conditions with the implementation of multi threading.
The client software was established by a windows service, which had the advantage to run in background and to get started with windows. Also the server program provides statistical evaluations for the administrator (E.g. internet usage, cpu usage,…) via diagramms.

Send an e-mail with C#

<p class="MsoNormal" style="MARGIN: 0in 0.9pt 0pt 17.6pt; LINE-HEIGHT: normal; tab-stops: .5in; mso-layout-grid-align: none"><span style="color: #000000;"><span style="FONT-SIZE: 11.5pt; FONT-FAMILY: 'Segoe UI','sans-serif'">We want to write a class that can be used to send an email to a selected email address. This class should have four properties namely: To, From, Subject, and Body, </span><span style="FONT-SIZE: 11.5pt; FONT-FAMILY: 'Segoe UI','sans-serif'; mso-ansi-language: DE" lang="DE">and a method to send the email (in the current version, attachements are not covered).<br /></span></span></p><p class="MsoNormal" style="MARGIN: 0in 0.9pt 0pt 17.6pt; LINE-HEIGHT: normal; tab-stops: .5in; mso-layout-grid-align: none">&nbsp;</p><p class="MsoNormal" style="MARGIN: 0in 0.9pt 0pt 17.6pt; LINE-HEIGHT: normal; tab-stops: .5in; mso-layout-grid-align: none">&nbsp;</p><p class="MsoNormal" style="MARGIN: 0in 0.9pt 0pt 17.6pt; LINE-HEIGHT: normal; tab-stops: .5in; mso-layout-grid-align: none">&nbsp;</p>
Subscribe to c#