|
I am beginner in Multi-threading in C# & confuse in current task.
My task is to read datatable and display datatable in C# console application using multi threading.
I have three threads and one datatable which have 50000 Rors.
All thread must start same time.
I want :
1st thread print 1st row.
2nd thread print 2nd row.
3rd thread print 3rd row.
again
1st thread print 4st row.
2nd thread print 5nd row.
3rd thread print 6rd row. Continue till last row.
How to implement this???
Is there any other technique to do this????
|
|
|
|
|
|
crystal report 10.0 is created with unicode fonts to display data(from Oracle DB) and text in VietNamese. Then I use PdfSharp.dll tool to export this report to pdf document. Everything is ok until I have a new issue to edit the report. And some text fields do not display properly VietNamese, like the machine (or the pdf engine) doesn't have this font. The other text fields which are the sasme font (Unicode) still work well. I changed 'times new roman', 'arial'... but not worked.
I also searched and someone told to uninstall Update for Microsoft Windows(KB3102429 and KB3104002) but it doesn't work with me.
I find that if I export from crystal report viewer to .rtf document and then open it in Office 2007 and choose save as PDF, then the PDF file now display properly. Just to show some clues.
My OS is window7x32
Please give me advice.
|
|
|
|
|
If you want to use a font that may not be available on the users' machine, you'd embed it in the PDF file. There's an example here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks Eddy. The matter is that the some text fields which the same font (times new roman) still display properly. while some ones also in times new roman does not display properly. If the pdf don't have that font, while it displays properly some of them?
And, my code to export from report document to pdf file like this:
rpt.ExportToDisk(ExportFormatType.PortableDocFormat, String.Format("C:\\pdf\\BrkFee{0}\\{1}", firmno, fName));
How can I apply your code suggestion into this? I don't have any PdfPage object. thks again.
|
|
|
|
|
I'm in the process of breaking the link we currently have between EF and our application code. In order to do this, I've created a collection of "Domain" objects to match our "Database" objects, so I have an Entity Framework Account type, and a Domain Account type, both of which have the same fields, notably an int AccountId field.
I've also implemented a simple repository to allow us to pull domain objects. The issue I'm having is I my Find method - the prototype of which is;
Find(Expression<Func<Domain.Account, bool>> predicate)
Obviously, this method deals with domain objects. However, in the EF specific implementation of the repo I'm working on, I can't simply apply the same predicate to a Where() since the predicate refers to domain types rather than database types.
To that end, I've been looking at the ExpressionVisitor class in order to "mutate" the expression into one that deals with database types. Since I want to change the Parameter, I've created the following implementation;
public class ExpressionParameterModificationVisitor<TTo> : ExpressionVisitor
{
public Expression Modify(Expression expression)
{
return this.Visit(expression);
}
protected override Expression VisitParameter(ParameterExpression expression)
{
return Expression.Parameter(typeof (TTo));
}
}
This appears to work as intended (based on examining what I get and my *limited* understanding of this), however, when I attempt to use it;
Expression<Func<Domaain.Account, bool>> predicate = account => account.AccountId == 1;
I'm seeing the error;
Additional information: Property 'Int32 AccountId' is not defined for type 'Data.Account'
The Data.Account type definitely has the correct field, so I'm assuming I need to do more with my Visitor, and override another method - the obvious suspect is to do the following;
protected override Expression VisitMember(MemberExpression node)
{
return Expression.MakeMemberAccess (base.Visit(node.Expression), typeof(TTo).GetProperty(node.Member.Name));
}
Now the issue I'm seeing is that the VisitMember method is called before the VisitParameter method, and hence I'm getting;
ParameterExpression of type 'Data.Account' cannot be used for delegate parameter of type 'Domain.Account'
Perhaps I'm approaching the issue incorrectly, but anyone have any ideas what I'm missing here?
C# has already designed away most of the tedium of C++.
|
|
|
|
|
This sort of expression rewriting can get very complicated. Something like this should work for fairly simple expressions:
public sealed class ExpressionParameterModificationVisitor<TFrom, TTo> : ExpressionVisitor
{
private readonly Stack<Dictionary<ParameterExpression, ParameterExpression>> _parameterMaps = new Stack<Dictionary<ParameterExpression, ParameterExpression>>();
protected override Expression VisitLambda<T>(Expression<T> node)
{
var parameters = VisitAndConvert<ParameterExpression>(node.Parameters, "VisitLambda");
var parameterMap = node.Parameters.Zip(parameters, (source, dest) => new { source, dest }).ToDictionary(p => p.source, p => p.dest);
_parameterMaps.Push(parameterMap);
try
{
var body = Visit(node.Body);
return Expression.Lambda(body, parameters);
}
finally
{
_parameterMaps.Pop();
}
}
protected override Expression VisitParameter(ParameterExpression expression)
{
if (typeof(TFrom) != expression.Type) return expression;
return Expression.Parameter(typeof(TTo), expression.Name);
}
private Expression MapParameter(Expression expression)
{
var param = expression as ParameterExpression;
if (param != null)
{
ParameterExpression result;
if (_parameterMaps.Count != 0 && _parameterMaps.Peek().TryGetValue(param, out result))
{
expression = result;
}
}
return expression;
}
protected override Expression VisitMember(MemberExpression node)
{
if (typeof(TFrom) != node.Expression.Type) return node.Update(Visit(node.Expression));
var expression = MapParameter(node.Expression);
var member = typeof(TTo).GetProperty(node.Member.Name);
return Expression.MakeMemberAccess(expression, member);
}
public static Expression<Func<TTo, TResult>> Modify<TResult>(Expression<Func<TFrom, TResult>> expression)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
var visitor = new ExpressionParameterModificationVisitor<TFrom, TTo>();
var result = visitor.Visit(expression);
return (Expression<Func<TTo, TResult>>)result;
}
}
NB: You need to maintain a stack of parameter maps to cope with nested lambdas - for example:
foo => foo.SomeCollection.Count(bar => bar.SomeFlag)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 31-Mar-16 11:48am.
|
|
|
|
|
Just a small kind of typo:
public static Expression<Func<TTo, TResult>> Modify<TResult>(Expression<Func<TFrom, TResult>> expression)
^^^^
Don't tell me you just typed that in Notepad!?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
LINQPad, but I changed the Modify method as I posted it.
Fixed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
As Richard's solution is working fine there's no practical need for this, but in case you're interested, for the purpose of learning about building Expressions: I answered a same, earlier question with a solution that does this without the use of ExpressionVisitor :
Mapping expressions Expression<Func<UserVM, object>> to Expression<Func<User, object>>[^]
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I'm trying to write a unit test with DeploymentItem attribute and in the code that is being tested I'm using Directory.GetFiles(directoryPath).
I tried to use DeploymentItem attribute to deploy the parent directory and have put directory name in directory path. However, it does not seem to work. Can you please point me out to doing it the proper way?
Thanks in advance.
|
|
|
|
|
|
Prior to .NET 4 there was a performance penalty (caused by boxing/unboxing) in using Enums as Keys in a Dictionary [^]; and there were some work-arounds, for example, this CP article: [^].
Comments on .NET 4 indicate the boxing is now not performed when Enum values are of the "simplest Type." I think that means when the values are 'Int rather than the other legal Types, but I am not sure about that.
I have been searching for a definitive statement on Enums and boxing in .NET 4.5 and above, but no luck so far. I wonder if an Enum using Flags would be handled in any different way (I doubt that).
thanks, Bill
«The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you.» Soren Kierkegaard
|
|
|
|
|
Looking at the source of the EqualityComparer<T> class[^], there's special-case code to handle enums where the underlying type is Byte , SByte , Int16 , UInt16 , Int32 , UInt32 , Int64 or UInt64 :
if (t.IsEnum) {
TypeCode underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(t));
switch (underlyingTypeCode) {
case TypeCode.Int16:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer<short>), t);
case TypeCode.SByte:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer<sbyte>), t);
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Byte:
case TypeCode.UInt16:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer<int>), t);
case TypeCode.Int64:
case TypeCode.UInt64:
return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer<long>), t);
}
}
The presence of the [Flags] attribute won't make any difference.
Obviously, this is the latest version of the BCL. I'm not sure how far back this was added.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Message Removed
modified 31-Mar-16 9:18am.
|
|
|
|
|
Hello,
i'am using the sync Framework to sync a local sdf with different Server databases (Oracle, Postgre).
Now i have a look for a newer versions or alternative solution.
I am going to use a local sqlite instead of the SQL Compact but i do not find current solutions to sync an sqlite against Oracle or Postgre.
Many Thanks
|
|
|
|
|
You can treat them as ODBC data sources and should be able to sync against them that way.
I know this works for Oracle at any rate, providing you have the Oracle client installed on the machine that will provide the data source.
|
|
|
|
|
I'm very sorry if I didn't post my code correctly. I am having trouble with this. The code is working to the directions specifications, but, I am not sure about one thing. How do avoid getting....lets say....6 "O's" and 3 "X's" when I run the program. That is not simulation. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tic_Tac_Toe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
const int ROWS = 3;
const int COLS = 3;
int[,] game = new int[ROWS, COLS];
Random rand = new Random();
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
game[row, col] = rand.Next(2);
}
if (game[0, 0] == 0)
{
label2.Text = "O";
}
else
{
label2.Text = "X";
}
if (game[0, 1] == 0)
{
label3.Text = "O";
}
else
{
label3.Text = "X";
}
if (game[0, 2] == 0)
{
label4.Text = "O";
}
else
{
label4.Text = "X";
}
if (game[1, 0] == 0)
{
label5.Text = "O";
}
else
{
label5.Text = "X";
}
if (game[1, 1] == 0)
{
label6.Text = "O";
}
else
{
label6.Text = "X";
}
if (game[1, 2] == 0)
{
label7.Text = "O";
}
else
{
label7.Text = "X";
}
if (game[2, 0] == 0)
{
label8.Text = "O";
}
else
{
label8.Text = "X";
}
if (game[2, 1] == 0)
{
label9.Text = "O";
}
else
{
label9.Text = "X";
}
if (game[2, 2] == 0)
{
label10.Text = "O";
}
else
{
label10.Text = "X";
}
if (game[0, 0] == 0 && game[0, 1] == 0 && game[0, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[0, 1] == 1 && game[0, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[1, 0] == 0 && game[1, 1] == 0 && game[1, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[1, 0] == 1 && game[1, 1] == 1 && game[1, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[2, 0] == 0 && game[2, 1] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[2, 0] == 1 && game[2, 1] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 0] == 0 && game[1, 0] == 0 && game[2, 0] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[1, 0] == 1 && game[2, 0] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 1] == 0 && game[1, 1] == 0 && game[2, 1] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 1] == 1 && game[1, 1] == 1 && game[2, 1] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 2] == 0 && game[1, 2] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 2] == 1 && game[1, 2] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 0] == 0 && game[1, 1] == 0 && game[2, 2] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 0] == 1 && game[1, 1] == 1 && game[2, 2] == 1)
{
label11.Text = "X Wins";
}
else if (game[0, 2] == 0 && game[1, 1] == 0 && game[2, 0] == 0)
{
label11.Text = "O Wins";
}
else if (game[0, 2] == 1 && game[1, 1] == 1 && game[2, 0] == 1)
{
label11.Text = "X Wins";
}
else
{
label11.Text = "DRAW";
}
}
}
}
}
|
|
|
|
|
The problem is that there's no context for the random values generation. You generate each 0 or 1 irrespective of how many there already are. So you need a method that ensures that there are either four 0's and five 1's or the other way around. There are several ways to do that. Here are two:
a) Create a one-dimensional array with a length of nine and fill it either with four or five 1's (the other indices are 0 by default and to decide whether it will be four or five 1's you can use Random.Next(4, 6) ). Then randomly swap the values of two indices multiple times. Then fill the values from that one-dimensional array into your two-dimensional game[3,3]-array.
b) Create an array of nine random numbers with no explicit upper bound (so the maximum value will be Int32.MaxValue). For the first four or five of those numbers, set the least significant bit (LSB) to 0 and for the remaining values, set it to 1. Then sort the array by values. Then fill the two-dimensional game-array based on the values from the sorted array but take only the value of the LSB.
There's something more to say about your code but, as I have to finish, I'll do that later if nobody else did until then.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Quite interesting code, I can see a fair bit wrong with it.
But, to your question of getting an equal balance of Xs and Os, why don't you just do it the way it's done, but taking it in turns?
That means choosing a random place to put you X or O (flip-flopped), rather than choosing a random X or O to put in an ordered place.
Regards,
Rob Philpott.
|
|
|
|
|
You can greatly simplify your code by storing "X" and "O" directly in game after changing it to a table of chars .
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
|
Hi All,
I have created console application to execute DB2 commands using C#. my commands executed fine and DB2 CLP command window opened i can see output as well. but once my command executed I am not able to close DB2 CLP window using C# code.
I tried to kill Process.GetProcessByName but not succeeded.
I also used Application.Exit() method.
Note: i am connecting to server DB2 using local machine.
Below is my code.
try
{
ProcessStartInfo objProcStartInfo;
Process objProcess;
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", "db2 connect to " + strMasterdatabaseName + " user " + strserviceAccountUserName + " using " + strserviceAccountPassword + "&db2 -f " + strexportFilePath + " > " + strexportLogFilePath + "");
objProcStartInfo.RedirectStandardOutput = false;
objProcStartInfo.UseShellExecute = false;
objProcStartInfo.CreateNoWindow = false;
objProcess = new Process();
objProcess.StartInfo = objProcStartInfo;
objProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
can someone please provide any direction on this?
Thanks
modified 30-Mar-16 12:49pm.
|
|
|
|
|
Message Closed
modified 30-Mar-16 15:05pm.
|
|
|
|
|
tried below code still DB2 CLP window remains open
using (objProcess = new Process())
{
try
{
//Connect To IBM DB2 database using CLP Process Object
//<tbd> Need to work on autoclose DB2 CLP
objProcStartInfo = new ProcessStartInfo("db2cmd.exe", "db2 connect to " + strMasterdatabaseName + " user " + strserviceAccountUserName + " using " + strserviceAccountPassword + "&db2 -f " + strexportFilePath + " > " + strexportLogFilePath + "");
objProcStartInfo.RedirectStandardOutput = false;
objProcStartInfo.UseShellExecute = false;
objProcStartInfo.CreateNoWindow = false;
objProcess.StartInfo = objProcStartInfo;
objProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
objProcStartInfo = null;
objProcess.Dispose();
}
}
}
|
|
|
|
|