Sequential Multi-Threading - multithreading

I wanna build Multi-Threaded procedure for the following code, but not able to correctly build it. I tried using Semaphore to sequentially complete the code but it only executed the myStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)])); part which comes under else part. The problem presents is that if we restore those streams into output filestream (mystream2) haphazardly, the file will corrupt.
Can anyone help me please.
for i := 1 to SL1.Count do
begin
if Length(SL1[i - 1]) > 12 then
begin
TempDir := SrcDir + 'temp_' +
IntToHex(Random(2147483647) + Random(2147483647), 8) + '\';
ForceDirectories(TempDir);
for k := 1 to 2 do
begin
tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp' + IntToStr(k);
fs := TFileStream.Create(tempfile, fmCreate or fmShareExclusive);
fs.CopyFrom(myStream1, StrToInt64('$' + DecodeRefStr(SL1[(i - 1)])[k]));
fs.Free;
end;
ExecAndWait(SrcDir + 'process.exe', Method + ' ' + '"' + TempDir +
IntToHex((i - 1), 8) + '.tmp1' + '" "' + TempDir +
IntToHex((i - 1), 8) + '.tmp2' + '" "' + TempDir +
IntToHex((i - 1), 8) + '.tmp4' + '"', TempDir,
True, True);
tempfile := TempDir + IntToHex((i - 1), 8) + '.tmp4';
fs := TFileStream.Create(tempfile, fmOpenRead);
myStream2.CopyFrom(fs, 0);
fs.Free;
TDirectory.Delete(TempDir, True);
end
else
myStream2.CopyFrom(myStream1, StrToInt64('$' + SL1[(i - 1)]));
end;
myStream1.Free;
myStream2.Free;

Related

How to set split() start and end delimiter?

[(0,
'0.011*"people" + 0.009*"christian" + 0.008*"god" + 0.008*"law" + '
'0.006*"believe" + 0.005*"question" + 0.005*"man" + 0.005*"life" + '
'0.005*"time" + 0.005*"write"'),
(1,
'0.014*"organization" + 0.013*"group" + 0.012*"image" + 0.010*"university" + '
'0.009*"program" + 0.008*"newsletter" + 0.007*"graphic" + '
'0.007*"information" + 0.007*"file" + 0.006*"box"'),
(2,
'0.015*"write" + 0.015*"organization" + 0.014*"article" + 0.012*"year" + '
'0.008*"university" + 0.007*"team" + 0.007*"time" + 0.006*"game" + '
'0.006*"give" + 0.006*"kid"'),
(3,
'0.049*"space" + 0.009*"year" + 0.008*"publish" + 0.006*"aerospace" + '
'0.006*"news" + 0.006*"technical" + 0.005*"satellite" + 0.005*"activity" + '
'0.005*"membership" + 0.005*"system"')]
How do I set the delimeter for the text file shown in the image? I want it to split into four separate text files. What and how should I give the start and end delimiter in the if() as can be seen in the code?. The text file has four separate parts 0,1,2,3. I am trying to write all the parts into separate text file.
`with open('topics.txt','r') as fo:
op=''
start=0
cntr = 1
for x in fo.read().split("\n"):
if (x==''):
if (start==1):
with open(str(cntr) + '.txt','w') as opf:
opf.write(op)
opf.close()
op=''
cntr+=1
else:
start=1
else:
if (op==''):
op = x
else:
op = op + '\n' + x
fo.close()`
If what you posted above is literally your text file, then this should give you each tuple separately.
I'm just using the regular expressions library. The pattern is just look for a left paren ( and run of anything that isn't a right paren, and then a right paren. Super simple.
import re
foo = """[(0,\n blahblahblah), (1,\n asdfasdf), (2,\n ghhgghiegiegieh)]"""
pat = r'\([^\)]+\)'
matches = re.findall(pat, foo)
['(0,\n blahblahblah)', '(1,\n asdfasdf)', '(2,\n ghhgghiegiegieh)']
If you want to separate out the numbers you can do that easily by spliting and striping out the extra stuff:
[i[1:-1].split(',\n')[1].strip() for i in matches]
#['blahblahblah', 'asdfasdf', 'ghhgghiegiegieh']
Then you can write them to whatever file you like.

Nodejs, asny to sync

I am quite new to NodeJs and need some help regarding flow.
So, I need to change some lib as it does not work for me. I have asny call and then right a way i have sync code. The thing is that sync code starts executing before asny part return data.
asny:
xmlenc.encrypt(message, options509, function(err, result) {
console.log("error:", err)
message = result
return message;
})
sync right after asyn:
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<" + envelopeKey + ":Envelope " +
xmlnsSoap + " " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
encoding +
this.wsdl.xmlnsInEnvelope + '>' +
((self.soapHeaders || self.security) ?
(
"<" + envelopeKey + ":Header>" +
(self.soapHeaders ? self.soapHeaders.join("\n") : "") +
(self.security && !self.security.postProcess ? self.security.toXML() : "") +
"</" + envelopeKey + ":Header>"
)
:
''
) +
"<" + envelopeKey + ":Body" +
(self.bodyAttributes ? self.bodyAttributes.join(' ') : '') +
(self.security && self.security.postProcess ? ' Id="_0"' : '') +
">" +
genXML() +
"</" + envelopeKey + ":Body>" +
"</" + envelopeKey + ":Envelope>";
How to solve this that the progrem with wait for asyn part to return before moving to sync part.
thank you

How to activate window of other application

I have HTTP server application (service application that is ran as desktop application) that runs other application and later one should activate window of yet another application. Computer X contains all applications. When I run http://192.168.16.21:225/command/TTaskType.ttTestTask from Opera web browser from computer X, my WinActivate function works. When I run http://192.168.16.21:225/command/TTaskType.ttTestTask from computer Y, window is not activated. None debug outputs returns errors (I get FLastErrorY printed). I even tried to run HTTP server as admin. What else could I try?
function WinActivate(const AWinTitle: string): boolean;
var
_WindowHandle: HWND;
_KeyboardState: TKeyboardState;
begin
ResetError;
_WindowHandle := FindWindow(nil, PWideChar(AWinTitle));
FLastError := GetLastError;
SetCursorPos(10, 12);
OutputDebugString(PWideChar('FLastError1: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if _WindowHandle <> 0 then
begin
//ShowWindow(_WindowHandle, SW_MINIMIZE);
if IsIconic(_WindowHandle) then
begin
ShowWindow(_WindowHandle, SW_RESTORE);
ResetError;
Result := IsIconic(_WindowHandle);
if Result then
Result := WinWaitActive(AWinTitle, 1000);
end
else
Result := SetForegroundWindow(_WindowHandle);
OutputDebugString(PWideChar('FLastErrorX: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if not Result then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastError2: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle])));
// Applications might lock focus, so, hack it around
GetKeyBoardState(_KeyboardState);
ShowWindow(_WindowHandle, SW_SHOWNORMAL);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE);
if _KeyboardState[VK_MENU] <> 1 then
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
ResetError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastErrorY: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
end;
end;
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
OutputDebugString(PWideChar('FLastError3: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
Result := FLastError = 0;
end;
if not Result then
begin
SetWindowPos(_WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
SetWindowPos(_WindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
end;
end;
Result := WinWaitActive(AWinTitle, 1000);
OutputDebugString(PWideChar('Dabar'));
end;
So, I have found out whats wrong. I knew that AutoHotkey haves window activation functionality. I have downloaded its source code and window activation code is well written and commented. So I have build a test application using AutoHotkey and my window was not activated. I found out it was because no there is no active user logged in.

Problematic formula with Apache POI

I'm using Java and generating an Excel file via Apache POI.
The following formula works perfect.
cell4.setCellFormula('(J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')')
Now I simply want to divide by the same H value eg. (J50-H50)/H50
cell4.setCellFormula('(J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')'+ '/H' + (itemCountSize + 3))
However it still just gives me (J50-H50)...
Any assistance would be appreciated.
Think you need an extra enclosing bracket
Have you tried:
cell4.setCellFormula('((J' + (itemCountSize + 3) + '-H' + (itemCountSize + 3)+ ')'+ '/H' + (itemCountSize + 3) + ')')
Or, simplifying with groovy string templating:
cell4.cellFormula = "((J${itemCountSize+3}-H${itemCountSize+3})/H${itemCountSize+3})" –

Javascript: Error in String Concatenation

So I have this javascript function:
function show_courseline (course_index, repeats) {
var s = "";
var count = 0;
while (count < repeats) {
s = s + 'Number: ' + document.getElementById(course_index + count + 'a').innerHTML + '\n' +
'Semester: ' + document.getElementById(course_index + count + 'b').innerHTML + '\n' +
'Year: ' + document.getElementById(course_index + count + 'c').innerHTML + '\n' +
'Title: ' + document.getElementById(course_index + count + 'd').innerHTML + '\n' +
'Units: ' + document.getElementById(course_index + count + 'e').innerHTML + '\n' +
'Description: ' + document.getElementById(course_index + count + 'f').innerHTML + '\n';
++count;
}
alert(s);
}
But I get an error when I run it through an "onclick" input box. It has nothing to do with the document ids not being there, because they are.
Is course_index integer? If it is course_index + count is an integer and I know that ids cannot start with a digit

Resources