|
hello every one,I'm trying to load and run an application A from memory which was written in c#.I have learn the skill from this http://www.codeproject.com/Articles/13897/Load-an-EXE-File-and-Run-It-from-Memory
It work well when the A's Form was simple,but when i add something control such as webbrowser to A's Form, it will throw an exception:"cannot be instantiated because the current thread is not in a single-threaded apartment" . below is my code
Assembly myAssembly = Assembly.Load(exeBuffer);
_MethodInfo myInfo = myAssembly.EntryPoint;
new Thread(() => myInfo.Invoke(null, null)).Start();
could somebody tell me how to solve it? thank you very much.
|
|
|
|
|
You should post your question in the forum at the end of the article, so the author can help you.
|
|
|
|
|
As the error message says, the new Thread needs to be set to STA :
Thread myThread = new Thread(() => myInfo.Invoke(null, null));
myThread.ApartmentState = ApartmentState.STA;
myThread.Start();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great,it works,thank you so much!
|
|
|
|
|
Hi,
if i use Calendar extender with textbox, Can i enter a date directly to the textbox or choose it from the Calendar
best regards
|
|
|
|
|
Note: the definition of the 'Student class, and the List<Student> 'studentList, used here is appended below.
Suppose we want a query function that, given a list of values-to-match, returns all instances of 'Student in the 'studentList which, for fields that are of Type List<>, have any of the values-to-match ?
Func<Student, List<int>, bool> MatchFromScoresList = (student, itemList)
=> student.Scores.Any(isMatchingEntry => itemList.Contains(isMatchingEntry));
Func<Student, List<string>, bool> MatchFromCommentsList = (student, itemList)
=> (student.Comments != null) && student.Comments.Any(isMatchingEntry
=> itemList.Contains(isMatchingEntry)); Okay, now we can use queries like:
var test1 = students.Where(student => MatchFromScoresList(student, new List<int>{60,91}));
var test2 = students.Where(student => MatchFromCommentsList(student, new List<string> { "one", "two"})); Question 1: is there any value to writing these function calls using the static 'Where of 'Enumerable ?
var test1 = Enumerable.Where(students, student
=> MatchFromScoresList(student, new List<int>{60,91})); Question 2: Suppose we wanted to define one Func to handle both matching Lists of Types Int, and String ? Yes: this is an intellectual exercise, probably unnecessary excursion into the impractical and esoteric ... until you tell me it's not ! In fact, I suspect using dynamic in a Func is not a good practice, and would lead to impaired performance, and, since it's easy to chain multiple Linq calls and do 'Joins ... well ... I still want to know
Clearly we can pass use a 'dynamic variable in the Func to get whatever Type of values-to-check List we want into the Func: these will work fine with the same two sample calls shown above:
Func<Student, dynamic, bool> dynMatchFromScoresList = (student, itemList)
=> student.Scores.Any(isMatchingEntry => itemList.Contains);
Func<Student, dynamic, bool> dynMatchFromCommentsList = (student, itemList)
=> (student.Comments != null) && student.Comments.Any(isMatchingEntry
=> itemList.Contains(isMatchingEntry)); But, how we could pass something into the Func that would tell it which Field in the data to compare to: Student.Scores, or Student.Comments ? The only way I can see to do that now is to use a boolean flag:
Func<Student, dynamic, bool, bool> dynMatchFromMultiTypeList = (student, itemList, isListType) =>
(isListType)
?
(student.Comments != null) && student.Comments.Any(isMatchingEntry
=> itemList.Contains(isMatchingEntry))
:
(student.Comments != null) && student.Comments.Any(isMatchingEntry
=> itemList.Contains(isMatchingEntry));
var test0 = Enumerable.Where(students, student => dynMatchFromMultiTypeList(student, new List<int> { 88, 91 }, true)); Is it possible to do some kind of Type checking in the body of the Func: perhaps you can enlighten me, thanks !
public class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
public List<int> Scores;
public List<string> Comments;
}
private List<Student> studentList;
// initialization
private void initializeStudentList()
{
// sample data copied from somewhere on one of MSDN's pages on Linq
studentList = new List<Student>
{
new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}, Comments = new List<string>{"one","two"}},
new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 60, 91, 70}},
new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}, Comments = new List<string>{"three","two"}},
new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
};
}
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
modified 21-Oct-14 21:59pm.
|
|
|
|
|
Question 1:
In the compiled code, there is no difference between calling an extension method as an extension method, and calling it as a static method.
The main benefit of calling it as an extension method is that it makes chained calls easier to read.
result = Enumerable.Select(Enumerable.OrderBy(Enumerable.Where(Enumerable.GroupBy(source, groupBy), filter), sorting), projection);
result = source.GroupBy(groupBy).Where(filter).OrderBy(sorting).Select(projection);
The only time you'll really need to call an extension method as a static method is when you're using dynamic , as the runtime binder doesn't know about extension methods.
Question 2:
You could try passing a list selector to your delegate. However, using dynamic will complicate matters - you can't call extension methods, and you can't use a dynamic parameter in a lambda method.
The only way I could get this to work with a dynamic parameter was to replace the inner lambda method with a call to .Intersect(list).Any() :
Func<Student, Func<Student, dynamic>, dynamic, bool> dynMatchFromMultiTypeList = (student, listSelector, itemList)
=> listSelector(student) != null && Enumerable.Any(Enumerable.Intersect(listSelector(student), itemList));
result = students.Where(student => dynMatchFromMultiTypeList(student, s => s.Scores, new List<int> { 88, 91 }));
However, I'm not sure that really gives you any benefit over:
result = students.Where(student => student.Scores != null && student.Scores.Intersect(new List<int> { 88, 91 }).Any());
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard, for your thoughtful comments, and code example !
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
If you want to get really fancy, you could throw in some functional programming concepts:
Func<Func<Student, dynamic>, Func<dynamic, Func<Student, bool>>> studentFilterFactory
= listSelector => itemList => student =>
listSelector(student) != null && Enumerable.Any(Enumerable.Intersect(listSelector(student), itemList));
Func<dynamic, Func<Student, bool>> filterByScoresFactory = studentFilterFactory(student => student.Scores);
Func<Student, bool> filterBySpecificScores = filterByScoresFactory(new List<int> { 88, 91 });
result = students.Where(filterBySpecificScores);
By caching the studentFilterFactory and filterByScoresFactory delegates, it then becomes much easier to filter a list of students with specific scores.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wow ! I am going to have fun studying this example. thanks, Bill
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
Hi,
I would like to ask how speedtest.net calculates the upload and download speed please?
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Nope. Not seeing a C# question in there.
|
|
|
|
|
Sorry, let me ask it this way...
I tried this code. I am getting the download rate in the result but how can I calculate the speed just like speedtest.net.
for example: I tried below code and got 69kb/sec and when I try the same time on speedtest.net the result will show 1.69Mbps.
How can I display similar result?
Uri URL = new Uri("http://www.jassimrahma.com/speedtest/1024kb.txt");
WebClient wc = new WebClient();
double starttime = Environment.TickCount;
wc.DownloadFile(URL, @"C:\temp\speedtest\speedtest.txt");
double endtime = Environment.TickCount;
double secs = Math.Floor(endtime - starttime) / 1000;
double secs2 = Math.Round(secs, 0);
double kbsec = Math.Round(1024 / secs);
lblStatus.Text = "Download rate: " + kbsec + " kb/sec";
try
{
System.IO.File.Delete(@"C:\temp\speedtest\speedtest.txt");
}
catch
{
}
Technology News @ www.JassimRahma.com
|
|
|
|
|
|
Some remarks:
- a 1kB text file is not a useful test object because:
- - text data can be compressed for transfer (some servers do that automatically if the browser accepts it)
- - it is terribly small, the overhead for setting up the communication may be large in comparison to the actual transfer time
- you could do some "ping" before and substract the average ping time from your measured transfer time
- beware of the difference in units: download/upload speed is usually given in BITS per second, not bytes per second - that makes a factor of 8.
My suggestion:
Use a large binary file with "random" data in it (compressed audio or image formats will normally do) whose transfer will take several seconds (then there is no need for the ping either).
|
|
|
|
|
|
//I have c# code in my form. Search button makes sql conn where I use stored proc to add params.
private void btnSearch_Click(object sender, EventArgs e){
string connectionString = "Data Source="" I verified correct conn String, it works ";
string commandText = "sp_Case_Search";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(commandText, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600;
conn.Open();
cmd.Parameters.Add(new SqlParameter("@Field", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("@Field", SqlDbType.VarChar, 50));
cmd.Parameters.Add(new SqlParameter("@Search", SqlDbType.VarChar, 500));
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
}
//The WHERE CASE @Field are radio buttons in the form where if one RB is selected populate the SELECT fields in a datarepeater. The commented out is what I tried, produces diff. errors.
ALTER PROCEDURE [dbo].[sp_Case_Search]
@Field varchar(50),
@Search varchar(500)
AS
BEGIN
SET NOCOUNT ON;
declare @Criteria varchar(502) = '%'+ @Search + '%'
SELECT
[ID]
,[EnterDate]
,[EnterTime]
,[EnterBy]
,[CaseTypeID]
,[MethodTypeID]
,[UpdateDate]
,[UpdateBy]
,[CaseDocNumber]
,[SenderAddress]
FROM tblNewCaseEntry
WHERE
CASE
When @Field = 'ServedBy' then ServedBy
When @Field = 'CaseDocNumber' then CaseDocNumber
When @Field = 'SenderAddress' then SenderAddress
When @Field = 'Claim' then Claim
End like @Criteria
ORDER BY EnterDate DESC;
END
PLEASE help to use @Field correctly in my code.
Maybe I am doing it ALL WRONG. Thank yoU!
|
|
|
|
|
You haven't specified a value for either of your parameters. Either set the Value property of the SqlParameter object, or use the AddWithValue method of the Parameters collection.
SqlParameter pField = new SqlParameter("@Field", SqlDbType.VarChar, 50);
pField.Value = parameterValueHere;
cmd.Parameters.Add(pField);
cmd.Parameters.Add(new SqlParameter("@Field", SqlDbType.VarChar, 50) { Value = parameterValueHere });
cmd.Parameters.AddWithValue("@Field", parameterValueHere);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
0) (Unrelated to the error) There is no need to specify the datatypes for the parameters; they will be set when you set the value.
1) If you don't set the Value, or if the value is NULL, set the Value to DBNull.Value
2) You may need to set the Direction of the parameters.
3) = edtPicker; and = udtPicker; -- Don't those need a .Value ?
3) DateTime edtPicker = Convert.ToDateTime(EnterdateDateTimePicker.Value); -- you don't need to convert; just cast
DateTime edtPicker = (DateTime) EnterdateDateTimePicker.Value;
4) The procedure returns a DataTable, are you expecting it to set your parameters?
Otherwise, it seems OK.
modified 21-Oct-14 16:09pm.
|
|
|
|
|
I am not clear if I need params or not, new to this. To start, I have a very simply search, display winform. Select a radio button, (ServedBy, CaseDocNumber, SenderAddress), hit Search, and storedProc populates fields(EnterDate, EnterTime,EnterBy,CaseTypeID, MethodTypID,UpdateDate,UpdateBy) in a datarepeater in same form. Do I just bind the controls to a datasource? I figure I need to do a sql conn first, then add params. The @Fields in the storedProc throws me off thinking I need to use that. Sorry if its confusing.
|
|
|
|
|
You need the @Field and @Search parameters, and you need to set their Values, but you don't need the others. Try using AddWithValue.
And you have two parameters named @Field.
modified 21-Oct-14 16:18pm.
|
|
|
|
|
cmd.Parameters.AddWithValue("@Field", ServedBy);
cmd.Parameters.AddWithValue("@Field", CaseDocNumber);
cmd.Parameters.AddWithValue("@Field", SenderAddress);
cmd.Parameters.AddWithValue("@Field", Claim);
cmd.Parameters.AddWithValue("@Search", ID);
cmd.Parameters.AddWithValue("@Search", EnterDate);
cmd.Parameters.AddWithValue("@Search", EnterTime);
cmd.Parameters.AddWithValue("@Search", EnterBy);
cmd.Parameters.AddWithValue("@Search", CaseTypeID);
cmd.Parameters.AddWithValue("@Search", MethodTypeID);
cmd.Parameters.AddWithValue("@Search", UpdateDate);
cmd.Parameters.AddWithValue("@Search", UpdateBy);
cmd.Parameters.AddWithValue("@Search", CaseDocNumber);
cmd.Parameters.AddWithValue("@Search", SenderAddress);
//My problem is I dont know how to use @Field and @Search with diff. values like above. If a search is done with any of the @Fields, I need to return the values of @Search. Sorry I'm dumb.
|
|
|
|
|
You can't have multiple parameters with the same name.
The procedure you showed requires @Field and @Search and it handles the rest.
|
|
|
|
|
hi
i am use 4 multicolumncombi in my form in first navigatorbinding is correct but when one record insert in database my moveprevious and next previous is not show correct tytle in multicolumncombobox and
when close program and run program again bindingnavigator is correct but when save again not show tytle correct
my source code:
[php]
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
moshtarakinhoghoghiBindingSource.AddNew();
if (phonebookDataSet.moshtarakin_hoghoghi.Compute("max(SysCode)+1", null).ToString() != null)
editBox1.Text = phonebookDataSet.moshtarakin_hoghoghi.Compute("max(SysCode)+1", null).ToString();
else
editBox1.Text = 1.ToString();
editBox3.Text = "";
editBox2.Text = "";
editBox4.Text = "";
multiColumnCombo1.Text = "";
multiColumnCombo2.Text = "";
multiColumnCombo3.Text = "";
multiColumnCombo4.Text = "";
uiComboBox1.Text = "";
}
[/php]
[php]
private void multiColumnCombo1_TextChanged(object sender, EventArgs e)
{
if (multiColumnCombo1.Text != "")
stateCityBindingSource.Filter = "State_Id='" + Convert.ToInt32(multiColumnCombo1.Value.ToString()) + "'";
}
private void multiColumnCombo3_TextChanged(object sender, EventArgs e)
{
if (multiColumnCombo3.Text != "")
activityBindingSource.Filter = "code_groh='" + Convert.ToInt32(multiColumnCombo3.Value.ToString()) + "'";
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{
moshtarakinhoghoghiBindingSource.EndEdit();
moshtarakin_hoghoghiTableAdapter.Update(this.phonebookDataSet.moshtarakin_hoghoghi);
}
catch
{
}
}
[/php]
. please help me .
best regards
modified 22-Oct-14 3:51am.
|
|
|
|
|
my problem resolved
my code:
[php]
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'phonebookDataSet.moshtarakin_hoghoghi' table. You can move, or remove it, as needed.
this.moshtarakin_hoghoghiTableAdapter.Fill(this.phonebookDataSet.moshtarakin_hoghoghi);
SqlConnection ConSub = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
ConSub.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("Select * from State", ConSub);
DataTable State = new DataTable();
Adapter.Fill(State);
multiColumnCombo1.DataSource = State;
Adapter.SelectCommand.Connection = ConSub;
Adapter.SelectCommand.CommandText = "Select * from StateCity where State_Id='" + Convert.ToInt32(multiColumnCombo1.Value.ToString()) + "'";
DataTable StateCity = new DataTable();
Adapter.Fill(StateCity);
multiColumnCombo2.DataSource = StateCity;
Adapter.SelectCommand.Connection = ConSub;
Adapter.SelectCommand.CommandText = "Select * from groh";
DataTable groh = new DataTable();
Adapter.Fill(groh);
multiColumnCombo3.DataSource = groh;
Adapter.SelectCommand.Connection = ConSub;
Adapter.SelectCommand.CommandText = "Select * from activity where code_groh='" + Convert.ToInt32(multiColumnCombo3.Value.ToString()) + "'";
DataTable activity = new DataTable();
Adapter.Fill(activity);
multiColumnCombo4.DataSource = activity;
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
moshtarakinhoghoghiBindingSource.AddNew();
if (phonebookDataSet.moshtarakin_hoghoghi.Compute("max(SysCode)+1", null).ToString() != null)
editBox1.Text = phonebookDataSet.moshtarakin_hoghoghi.Compute("max(SysCode)+1", null).ToString();
else
editBox1.Text = 1.ToString();
editBox3.Text = "";
editBox2.Text = "";
editBox4.Text = "";
multiColumnCombo1.Text = "";
multiColumnCombo2.Text = "";
multiColumnCombo3.Text = "";
multiColumnCombo4.Text = "";
uiComboBox1.Text = "";
}
private void multiColumnCombo1_TextChanged(object sender, EventArgs e)
{
if (multiColumnCombo1.Text != "")
{
SqlConnection ConSub = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
ConSub.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("", ConSub);
Adapter.SelectCommand.Connection = ConSub;
Adapter.SelectCommand.CommandText = "Select * from StateCity where State_Id='" + Convert.ToInt32(multiColumnCombo1.Value.ToString()) + "'";
DataTable StateCity = new DataTable();
Adapter.Fill(StateCity);
multiColumnCombo2.DataSource = StateCity;
}
}
private void multiColumnCombo3_TextChanged(object sender, EventArgs e)
{
if (multiColumnCombo3.Text != "")
{
SqlConnection ConSub = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
ConSub.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("", ConSub);
Adapter.SelectCommand.Connection = ConSub;
Adapter.SelectCommand.CommandText = "Select * from activity where code_groh='" + Convert.ToInt32(multiColumnCombo3.Value.ToString()) + "'";
DataTable activity = new DataTable();
Adapter.Fill(activity);
multiColumnCombo4.DataSource = activity;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{
moshtarakinhoghoghiBindingSource.EndEdit();
moshtarakin_hoghoghiTableAdapter.Update(this.phonebookDataSet.moshtarakin_hoghoghi);
}
catch
{
}
}
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
if (moshtarakinhoghoghiBindingSource.Count > 0)
moshtarakinhoghoghiBindingSource.RemoveCurrent();
moshtarakinhoghoghiBindingSource.EndEdit();
moshtarakin_hoghoghiTableAdapter.Update(this.phonebookDataSet.moshtarakin_hoghoghi);
}
[/php]
|
|
|
|
|