|
Seems like a simple check for ActiveDocument being open is in order here.
There's got to be some property to check to see if there is a document open. Experiment. Look around.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I get what you're saying but what I'm asking is that, why is the DocumentChange event being fired when there is no open document?
|
|
|
|
|
I doubt anyone would be able to tell you that. Software development is full of things like this. The best thing you can do is find a workaround, such as checking to see if there's a document open.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thank you so this problem is not due to a bug in the code I have written right?
|
|
|
|
|
If you look at this line, it's looking for an ActiveDocument. So, if you have no document open, it's not going to work.
|
|
|
|
|
yes, I understand the error.. but that line is inside an event handler.. I'm using the DocumentChange event to trigger the event handler.. But why is the event handler getting triggered even before a document is opened?
|
|
|
|
|
Are you really sure you want to use this event? If you open two documents, and switch backwards and forwards between them, this event will be thrown every time, so the message box will display repeatedly.
|
|
|
|
|
Yep I'm very sure.. But why is the event fired when there is no open document?
|
|
|
|
|
Have you checked this is the only instance of Word running?
|
|
|
|
|
Yes.. I'm sure its the only instance of word running..
|
|
|
|
|
Maybe the document's state is changing from closed to open. Also the handler refers to the active document. Maybe it's opend first and activated afterwards. So when the first document is opened, it may not be active yet.
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
No.. I've not yet chosen the document to open at all.. Error pops up when the application is launched. anyway.. please confirm that its not a problem with the code..
|
|
|
|
|
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
|
|
|
|
|