Saturday, 31 August 2013

Java start while loop without pausing current thread

Java start while loop without pausing current thread

I have a GUI, in which when a button is clicked a while loop has to be
started. But upon starting the while loop the GUI will be paused until the
while loop has been terminated. Is there any way to circumvent this?

How to use DateTime.UtcNow inside javascript function to display javascript clock?

How to use DateTime.UtcNow inside javascript function to display
javascript clock?

There is a pretty simple javascript clock like below
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById('clockSpan').innerHTML = h + ":" + m + ":"
+ s;
t = setTimeout(function () { startTime() }, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
</script>
But it uses browser time settings. Instead i want to use server time.
How can i do something like this ?
var today = new Date(<%=DateTime.UtcNow%>);
It gives error like this

asp.net 4.5

Viewcontroller not loading

Viewcontroller not loading

Working on a tabbed ipad application. My program builds but fails to
launch with the error given below on this line from the AppDelegate.m,
self.tabBarController.viewControllers = @[viewController1, viewController2];
This code has not been edited ie straight from the template and has worked
correctly until now. The view controllers seem to initiate, and show valid
properties in the inspector when debugging. I am assuming the NSrange
error is referring to the self.tabBarController.viewControllers array,
maybe that's not true.
It's hard to imagine how I caused this error to occur. Any suggestions
gratefully accepted, particularly obtaining more info to isolate the
error.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc]
initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1,
viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
* Terminating app due to uncaught exception 'NSRangeException', reason: '*
-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array

Friday, 30 August 2013

How can an exception class obtain the object that raised it?

How can an exception class obtain the object that raised it?

I want one of my exception classes to be able to obtain the object in
whose context the exception was raised. For example, in the
semi-pseudocode
class Foo
def zorch!
raise MyException.new("d'oh!")
end
end
c = Foo.new
c.zorch!
I want MyException#initialize to be able to obtain the object c.
This can probably be done somehow using self.send(:binding) in the
#initialize method, but I haven't figured out how yet:
Kernel.eval('self', self.send(:binding)).class.name
=> MyException # *not* 'Foo'
Which makes sense, since MyException hasn't been fully instantiated yet
and self.send(:binding) refers to the current context rather than the
caller's.
So how can I access the caller's context/binding?

Thursday, 29 August 2013

Passing IDbSet to generic method

Passing IDbSet to generic method

I'm sure this is pretty basic but I have trouble constructing generic
methods. I have numerous interfaces declared in my DBContext class
(IDbSet, IDbSet, etc.). I want to pass them into a method that will
process them. In this case by deleting records. This is what I tried to do
but I can't figure out how to create the method signature.
private static void EmptyTable<T>(ContactContext context, T records)
{
foreach (var record in records)
{
records.Remove(record);
}
context.SaveChanges();
}

stringstream sometimes won't reach the end of a file

stringstream sometimes won't reach the end of a file

I wrote a c++ program to manupilate data files. However in 5 files out of
100, the stringstream can't reach the end of the file. I ran tests within
the following chunk of code:
// ssFile contains the whole text in each file.
// chunk is a string.
// value is an int
// tId and tName are both string vectors.
loop = 0;
while(ssFile >> chunk)
{
if(ssFile.eof()) cout << "\nEOF!\n" << endl;
if( chunk == "<frame>" )
{
ssFile >> value;
cout << "-> " << loop << ": " << chunk << value;
{
stringstream ssVal;
ssVal << value;
tId.push_back(ssVal.str());
}
loop++;
ssFile >> chunk;
tName.push_back(chunk);
cout << "= " << chunk << endl;
}
}// while (ssFile >> chunk)
In these 5 particular files, the ssFile sstream reaches no end at all: the
loop is endlessly executed. I checked these txt files but their end
doesn't seem to be different from the others. Does it come from the
stringstreams (which I doubt)? Or more probably from these files? If so
how are end of files characters represented, perhaps these file are
missing one?
Edit: added an EOF test and the following output:
-> 1: <frame>1= flying
-> 2: <frame>2= flying
-> 3: <frame>3= flying
-> 4: <frame>4= flying
-> 5: <frame>5= flying
-> 6: <frame>10= hiting
-> 7: <frame>11= hiting
-> 8: <frame>12= hiting
-> 9: <frame>13= hiting
-> 10: <frame>20= hit
-> 11: <frame>21= hit
-> 12: <frame>22= hit
-> 13: <frame>23= hit
-> 14: <frame>30= rebounding
-> 15: <frame>31= rebounding
-> 16: <frame>32= rebounding
-> 17: <frame>33= rebounding
EOF!
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
Actually the loop is not executed again, the program just happens to be
stuck at the last occurence... So there is no infinite loop/output. THe
end of file seems to be detected, but still the program does not get out
of the loop. Then it tells me a bad allocation has been made.

Wednesday, 28 August 2013

Lua C API: embedding Lua into DLL project

Lua C API: embedding Lua into DLL project

I've got C++ project which is .DLL, I include there lua headers such as
lua.h, lualib.h and lauxlib.h I can execute script from there but when I
try to call some function it isn't called. Is it working this way or I am
doing something wrong? Thanks in advance.