I am working on flutter desktop app. I want to execute only single instance of app. But currently it allows me to run more than one instance. How can I allow only one .exe file of this application to run?
This is the customization in default flutter windows application properties, so we have to code in C++ for that purpose. A single window application instance can be achieved using a Mutex:
HANDLE hMutexHandle=CreateMutex(NULL, TRUE, L"my.mutex.name");
HWND handle=FindWindowA(NULL, "Test Application");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
WINDOWPLACEMENT place = { sizeof(WINDOWPLACEMENT) };
GetWindowPlacement(handle, &place);
switch(place.showCmd)
{
case SW_SHOWMAXIMIZED:
ShowWindow(handle, SW_SHOWMAXIMIZED);
break;
case SW_SHOWMINIMIZED:
ShowWindow(handle, SW_RESTORE);
break;
default:
ShowWindow(handle, SW_NORMAL);
break;
}
SetWindowPos(0, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(handle);
return 0;
}
Opening the win32_window.cpp file and adding this code snippet at the start in CreateAndShow() method will restrict the application to a single instance.
I am getting a compilation error.
The solution is this line at the end of the if block.
ReleaseMutex(hMutexHandle);
open windows/runnner/win32_cpp:
// add this function above CreateAndShow
bool CheckOneInstance()
{
HANDLE m_hStartEvent = CreateEventW( NULL, FALSE, FALSE, L"Global\\yourpackage" );
if(m_hStartEvent == NULL)
{
CloseHandle( m_hStartEvent );
return false;
}
if (GetLastError() == ERROR_ALREADY_EXISTS) {
CloseHandle( m_hStartEvent );
m_hStartEvent = NULL;
// already exist
// send message from here to existing copy of the application
return false;
}
// the only instance, start in a usual way
return true;
}
bool Win32Window::CreateAndShow(const std::wstring& title,
const Point& origin,
const Size& size) {
//Add the check
if( !CheckOneInstance()){
return false;
}
Destroy();
....
}
Related
I am using text/scanner package to parse some arbitrary expressions. I am currently trying to implement a not in option, that is, if the current identifier is not, and the next is in, parse it using function notin(left, right), and otherwise we parse it as negate(right).
I've essentially got the code to manage these cases however, I am unable to rewind the scanner in case the next token is not in. I've tried by recording the position and then reassigning it later, but to no avail and haven't been able to find a different solution.
func readToken(stream *scanner.Scanner) {
switch stream.Scan() {
case scanner.Ident:
switch stream.TokenText() {
case "in":
in(left, right)
case "not":
oldPosition := stream.Position
nextToken := stream.Scan()
if nextToken == scanner.Ident {
switch stream.TokenText() {
case "in":
fmt.Println("notin")
default:
// how do we rewind the scanner?
stream.Position = oldPosition
fmt.Println("negate default")
}
} else {
fmt.Println("negate no-ident")
}
}
}
}
How can I rewind the scanner when I don't find a valid identifier?
Edit, I also tried using Peek() as below, but that still changes the state to the point that I'd need to rewind as well.
// other code
case "not":
nextIdent, err := getNextIdent(stream)
if err != nil {
fmt.Println("negate no-ident")
} else {
switch nextIdent {
case "in":
fmt.Println("notin")
default:
fmt.Println("negate default")
}
}
// other code
func getNextIdent(s *scanner.Scanner) (string, error) {
var nextIdent string
ch := s.Peek()
// skip white space
for s.Whitespace&(1<<uint(ch)) != 0 {
ch = s.Next()
}
if isIdentRune(ch, 0) {
nextIdent = string(ch)
ch = s.Next()
nextIdent += string(ch)
for i := 1; isIdentRune(ch, i); i++ {
ch = s.Next()
if s.Whitespace&(1<<uint(ch)) != 0 {
break
}
nextIdent += string(ch)
}
return nextIdent, nil
}
return "",errors.New("not a ident")
}
Note, the code I've got is a forked from Knetic/govaluate combined with a PR from GH user generikvault and some other forks. The full code can be found on my Github profile
By looking at the API references of text/scanner, I can't seem to find a way to rewind the scanner the way you want.
However, the Peek() method would get you the next rune without advancing the scanner. Inside the "not" case, you can use it to peek in advance to see if it matches.
Tried many time and many solution but all give me the same problem. Everything works just fine, the app works and no issues with it, it's just the shourtcut that is not being created on Desktop. I can see from the log files 'EmulateFileIOPermissionChecks' and a message error 'The given path's format is not supported.' but not sure what it means. I experienced the same error with this module "electron-squirrel-startup". Here is what is happening related to SquirrelSetup.log
2018-06-28 17:56:15> Program: Starting Squirrel Updater: --createShortcut C:\Users\name\AppData\Local\myApp\app-0.1.0\myapp.exe
2018-06-28 17:56:15> ApplyReleasesImpl: About to create shortcuts for
C:\Users\user\AppData\Local\myApp\app-0.1.0\myapp.exe, rootAppDir
C:\Users\user\AppData\Local\idpAuthElectronApp 2018-06-28 17:56:15> Unhandled exception: System.NotSupportedException: The given path's format is not supported. atSystem.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath) at
System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess
access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean
checkHost)
at System.IO.Directory.CreateDirectory(String path)
And Here is my code:
var handleSquirrelEvent = function() {
if (process.platform != 'win32') {
return false;
}
function executeSquirrelCommand(args, done) {
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var child = cp.spawn(updateDotExe, args, { detached: true });
child.on('close', function(code) {
done();
});
};
function install(done) {
var target = path.resolve(process.execPath);
executeSquirrelCommand(['--createShortcut', target], done);
};
function uninstall(done) {
var target = path.resolve(process.execPath);
executeSquirrelCommand(["--removeShortcut", target], done);
};
var squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
install(app.quit);
return true;
case '--squirrel-updated':
install(app.quit);
return true;
case '--squirrel-obsolete':
app.quit();
return true;
case '--squirrel-uninstall':
uninstall(app.quit);
return true;
}
return false;
};
if (handleSquirrelEvent()) {
return;
}
I have a form with a progress bar and a button that uploads an xml to a server.
While the button is pressed a new thread is created that creates a socket and then it sends the data to the server in chunks and in the meanwhile it updates the progress bar.
Now, when the upload button is pressed for a second time i get an access violation and in the debugger the address of the Progress Bar object is NULL.
I can't figure out why the Progress Bar is getting freed so if anyone has any idea i would be grateful.
P.S. The target OS is windows
P.S.2 If the same code runs on the main thread without the usage of a thread then i don't seem to have this issue and even if i skip the usage of the progress bar in overall in the thread it is set to null again after the first push of the upload button.
The thread Constructor:
__fastcall UploadRouteThread::UploadRouteThread(bool CreateSuspended) : TThread(CreateSuspended)
{
this->OnTerminate = OnTerminateHandler;
ioHandlerStack = new TIdIOHandlerStack();
tcpClient = new TIdTCPClient();
tcpClient->ReadTimeout = -1;
tcpClient->UseNagle = true;
tcpClient->IOHandler = ioHandlerStack;
tcpClient->OnConnected = OnConnectedHandler;
}
The OnTerminate handler:
void __fastcall UploadRouteThread::OnTerminateHandler(TObject *Sender)
{
TabbedwithNavigationForm->UploadButton->Text = "Upload";
TabbedwithNavigationForm->UploadButton->Enabled = false;
TabbedwithNavigationForm->ProgressBar->Visible = false;
tcpClient->DisconnectNotifyPeer();
ShowMessage("Data uploaded.");
delete ioHandlerStack;
delete tcpClient;
TabbedwithNavigationForm->OptionButton->Enabled = true;
TabbedwithNavigationForm->RetrieveRoutesButton->Enabled = true;
TabbedwithNavigationForm->TrackButton->Enabled = true;
TabbedwithNavigationForm->MediaButton->Enabled = true;
}
The Execute method:
void __fastcall UploadRouteThread::Execute()
{
FreeOnTerminate = true;
tcpClient->Connect();
}
Two supplumentary functions:
void __fastcall UploadRouteThread::SetHostPort(UnicodeString host, unsigned short port)
{
tcpClient->Host = host;
tcpClient->Port = port;
}
void __fastcall UploadRouteThread::SetXML(AnsiString xmlString)
{
this->xmlString = xmlString;
}
The OnConnect Handler:
void __fastcall UploadRouteThread::OnConnectedHandler(TObject *Sender)
{
NextPacketSize nps;
TIdBytes bytes;
int chunks;
int bytesLength;
nps.PacketID = BasicPacket::DATA_UPLOAD;
nps.size = xmlString.Length();
tcpClient->IOHandler->WriteDirect(RawToBytes(&nps, sizeof(nps)), sizeof(NextPacketSize));
bytes = RawToBytes(xmlString.c_str(), xmlString.Length());
bytesLength = bytes.get_length();
chunks = ceil(float(bytesLength) / 256.0);
int previousSizeSent(0);
for(int i = 1; i <= chunks; i++)
{
if(Terminated)
break;
int bytesToSend = 256;
TByteDynArray byteDynArray;
if((bytesToSend > bytesLength))
{
bytesToSend = bytesLength;
}
byteDynArray = bytes.CopyRange(previousSizeSent, bytesToSend);
tcpClient->IOHandler->WriteDirect(ToBytes(byteDynArray, byteDynArray.get_length(), 0),
byteDynArray.get_length());
sent = (float(i) / float(chunks)) * 100;
TThread::Synchronize(this, UpdateProgressBarInternal);
previousSizeSent += bytesToSend;
bytesLength -= bytesToSend;
}
}
And the Update method for the progress bar:
void __fastcall UploadRouteThread::UpdateProgressBarInternal()
{
if(!TabbedwithNavigationForm->ProgressBar->Visible)
{
TabbedwithNavigationForm->ProgressBar->Visible = true;
TabbedwithNavigationForm->ProgressBar->Max = 100;
}
TabbedwithNavigationForm->ProgressBar->Value = sent;
}
I don't see anything in this code that would cause the ProgressBar pointer to become NULL. So either you are corrupting memory, or something else in other code not shown here is the culprit. Either way, to troubleshoot this you can run your app in the IDE debugger and set a Data Breakpoint on the ProgressBar variable before you run your thread for the first time. If something changes the value of that pointer, the breakpoint will be hit, and you can look at the call stack to figure out what is happening.
With that said, your thread is not very well organized. And there is a much simpler way to handle the chunking - let Indy do it for you. It has an OnWork event that you can use for your ProgressBar updates.
Try something more like this:
__fastcall UploadRouteThread::UploadRouteThread(String host, TIdPort port, AnsiString xmlString)
: TThread(false)
{
this->FreeOnTerminate = true;
this->OnTerminate = OnTerminateHandler;
this->xmlString = xmlString;
tcpClient = new TIdTCPClient();
tcpClient->Host = host;
tcpClient->Port = port;
tcpClient->UseNagle = true;
tcpClient->OnWork = OnWorkHandler;
}
__fastcall UploadRouteThread::~UploadRouteThread()
{
delete tcpClient;
}
void __fastcall UploadRouteThread::OnTerminateHandler(TObject *Sender)
{
TabbedwithNavigationForm->UploadButton->Text = "Upload";
TabbedwithNavigationForm->UploadButton->Enabled = false;
TabbedwithNavigationForm->ProgressBar->Visible = false;
if (FatalException)
ShowMessage("Data not uploaded.");
else
ShowMessage("Data uploaded.");
TabbedwithNavigationForm->OptionButton->Enabled = true;
TabbedwithNavigationForm->RetrieveRoutesButton->Enabled = true;
TabbedwithNavigationForm->TrackButton->Enabled = true;
TabbedwithNavigationForm->MediaButton->Enabled = true;
}
void __fastcall UploadRouteThread::OnWorkHandler(TObject *ASender, TWorkMode AWorkMode, __int64 AWorkCount)
{
if (Terminated)
Sysutils::Abort();
sent = (double(AWorkCount) * 100.0) / xmlString.Length();
// consider using TThread::Queue() instead so that you don't block
// the upload waiting for the UI to be updated...
TThread::Synchronize(this, &UpdateProgressBarInternal);
}
void __fastcall UploadRouteThread::Execute()
{
tcpClient->Connect();
try
{
NextPacketSize nps;
nps.PacketID = BasicPacket::DATA_UPLOAD;
nps.size = xmlString.Length();
tcpClient->IOHandler->Write(RawToBytes(&nps, sizeof(nps)));
tcpClient->BeginWork(wmWrite, xmlString.Length());
tcpClient->IOHandler->Write(RawToBytes(xmlString.c_str(), xmlString.Length()));
tcpClient->EndWork(wmWrite);
/* alternatively:
TIdMemoryBufferStream *strm = new TIdMemoryBufferStream(xmlString.c_str(), xmlString.Length());
try
{
// optional
tcpClient->IOHandler->SendBufferSize = 256;
// this calls (Begin|End)Work() internally...
tcpClient->IOHandler->Write(strm, 0, false);
}
__finally
{
delete strm;
}
*/
}
__finally
{
tcpClient->Disconnect();
}
}
void __fastcall UploadRouteThread::UpdateProgressBarInternal()
{
if (!TabbedwithNavigationForm->ProgressBar->Visible)
{
TabbedwithNavigationForm->ProgressBar->Visible = true;
TabbedwithNavigationForm->ProgressBar->Max = 100;
}
TabbedwithNavigationForm->ProgressBar->Value = sent;
}
I've been trying to save file from server to an ios device using urlstream but it doesn't work (it works fine on android devices . I tried using(documentsDirectory) but it doesn't work too .I used many other methods like (file.download ) and others but none is working . Any help please
I am using flash pro cs6 .
script sample :
import flash.filesystem.*;
import flash.events.ProgressEvent;
var urlString:String = "http://example.sample.mp3";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
urlStream.load(urlReq);
function loaded(event:Event):void {
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeAirFile();
}
function writeAirFile():void {
var file:File = File.applicationStorageDirectory.resolvePath("sample.mp3");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
}
function progressHandler(event:Event):void {
trace ("progressHandler: " + event);
}
Tested on iOS
_urlString = "http://example.sample.mp3";
_urlReq = new URLRequest(_urlString);
_urlStream = new URLStream();
_urlStream.addEventListener(flash.events.ProgressEvent.PROGRESS, progressHandler, false, 0, true);
_urlStream.addEventListener(flash.events.Event.COMPLETE, saveFileToDisc, false, 0, true);
_urlStream.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler, false, 0, true);
_urlStream.load(_urlReq);
private function progressHandler(evt:flash.events.ProgressEvent):void {
trace("progress: " + event.target.progress);
}
private function errorHandler(evt:flash.events.IOErrorEvent):void {
//do something
}
private function saveFileToDisc(event:flash.events.Event):void {
_fileData = new ByteArray();
_urlStream.readBytes(_fileData, 0, _urlStream.bytesAvailable);
_file = File.applicationStorageDirectory.resolvePath("sample.mp3");
_file.preventBackup = true;
_writeFileStream.addEventListener(flash.events.IOErrorEvent.IO_ERROR, filestreamErrorHandler, false, 0, true);
_writeFileStream.addEventListener(flash.events.Event.CLOSE, fileSaved, false, 0, true);
_writeFileStream.openAsync(_file, FileMode.UPDATE);
_writeFileStream.writeBytes(_fileData, 0, _fileData.length);
_writeFileStream.close();
}
private function filestreamErrorHandler(evt:flash.events.IOErrorEvent):void {
//do something
}
private function fileSaved(closeEvent:flash.events.Event):void {
//trace("file saved");
_writeFileStream.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, filestreamErrorHandler);
_writeFileStream.removeEventListener(flash.events.Event.CLOSE, fileSaved);
_urlString = null;
_urlReq = null;
_urlStream = null;
_file = null;
_fileData.length = 0;
_fileData = null;
}
Two obvious problems:
This line: urlStream..addEventListener(ProgressEvent.PROGRESS, progressHandler); has ' .. ' which is wrong.
Also you never write anything into your ByteArray.
set the 3rd parameter to 0 to make sure to read the entire data:
urlStream.readBytes(fileData, 0, 0); //0 = read all
Besides URLStream is not meant for that type of operation (it is meant to stream the loading of binary data). I personally do this using URLLoader (loading in binary) and everything works perfectly and save copy on folder.
I'm currently porting a pretty basic gallery application from PHP to Go. This application features automatic generation of thumbnails and middle-sized version of every image.
In PHP I used GD, because it ships with it and worked pretty well. (Code is at the end of the question). I thought I could just replicate that in Go and found go-gd from https://github.com/bolknote/go-gd (again, code is at the end). It works, but it is roughly 10 times slower (measured using time wget $URL). The PHP implementation takes about 1 second for generating a 1024x768 version from a 10 MP-image, while the Go-Code takes almost 10 seconds.
Is there any way to speed this up or any other image-processing libary for Go, which implements scaling and convolution while being reasonably fast?
PHP-Code
public function saveThumb($outName, $options) {
$this->img = imagecreatefromjpeg($filename);
if (!is_dir(dirname($outName))) {
mkdir(dirname($outName), 0777, true);
}
$width = imagesx($this->img);
$height = imagesy($this->img);
if ($options["keep_aspect"]) {
$factor = min($options["size_x"]/$width, $options["size_y"]/$height);
$new_width = round($factor*$width);
$new_height = round($factor*$height);
} else {
$new_width = $options["size_x"];
$new_height = $options["size_y"];
}
// create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
// copy and resize old image into new image
imagecopyresampled($tmp_img, $this->img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if ($options["sharpen"]) {
// define the sharpen matrix
$sharpen = array(
array(-1, -1.7, -1),
array(-1.7, 20, -1.7),
array(-1, -1.7, -1)
);
// calculate the sharpen divisor
$divisor = array_sum(array_map('array_sum', $sharpen));
// apply the matrix
imageconvolution($tmp_img, $sharpen, $divisor, 0);
}
// save thumbnail into a file
imagejpeg($tmp_img, $outName);
}
Go-Code
func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
targetFilename := entry.Filename(imageType)
sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
targetDirname, _ := filepath.Split(targetFilename)
os.MkdirAll(targetDirname, 0777)
targetFi, errT := os.Stat(targetFilename)
sourceFi, errS := os.Stat(sourceFilename)
image := gd.CreateFromJpeg(sourceFilename)
if image == nil {
return os.NewError("Image could not be loaded")
}
var targetX, targetY int = 0, 0
if options.KeepAspect {
factor := math.Fmin(float64(options.SizeX)/float64(image.Sx()), float64(options.SizeY)/float64(image.Sy()))
targetX = int(factor*float64(image.Sx()))
targetY = int(factor*float64(image.Sy()))
} else {
targetX = options.SizeX
targetY = options.SizeY
}
tmpImage := gd.CreateTrueColor(targetX, targetY)
image.CopyResampled(tmpImage, 0, 0, 0, 0, tmpImage.Sx(), tmpImage.Sy(), image.Sx(), image.Sy())
if options.Sharpen {
sharpenMatrix := [3][3]float32{
{-1, -1.7, -1},
{-1.7, 20, -1.7},
{-1, -1.7, -1} }
tmpImage.Convolution(sharpenMatrix, 9.2, 0)
}
tmpImage.Jpeg(targetFilename, 90)
return nil
}
EDIT: Go-Code using resize.go (see answer)
func (entry *entry) GenerateThumb(options ImageType, overwrite bool) os.Error {
targetFilename := entry.Filename(imageType)
sourceFilename := entry.Filename(IMAGE_TYPE_FULL)
targetDirname, _ := filepath.Split(targetFilename)
os.MkdirAll(targetDirname, 0777)
targetFi, errT := os.Stat(targetFilename)
sourceFi, errS := os.Stat(sourceFilename)
if errT == nil && errS == nil {
if targetFi.Mtime_ns > sourceFi.Mtime_ns && !overwrite {
// already up-to-date, nothing to do
return nil
}
}
log.Printf("Generate(\"%v\", %v)\n", imageType, overwrite)
inFile, fErr := os.Open(sourceFilename)
if fErr != nil {
log.Fatal(fErr)
}
defer inFile.Close()
img, _, err := image.Decode(inFile)
if err != nil {
log.Fatal(err)
}
var targetX, targetY int
if options.KeepAspect {
factor := math.Fmin(float64(options.SizeX)/float64(img.Bounds().Max.X), float64(options.SizeY)/float64(img.Bounds().Max.Y))
targetX = int(factor*float64(img.Bounds().Max.X))
targetY = int(factor*float64(img.Bounds().Max.Y))
} else {
targetX = curType.SizeX
targetY = curType.SizeY
}
newImg := resize.Resample(img, image.Rect(0, 0, img.Bounds().Max.X, img.Bounds().Max.Y), targetX, targetY)
var outFile *os.File
outFile, fErr = os.Create(targetFilename)
if fErr != nil {
log.Fatal(fErr)
}
defer outFile.Close()
err = jpeg.Encode(outFile, newImg, &jpeg.Options{90})
if err != nil {
log.Fatal(err)
}
return nil
}
You should check out this resize library: github.com/nfnt/resize. It has 6 good interpolation functions to choose from.
The Moustachio example application for GAE by Andrew Gerrand contains a resize.go file with a native Go implementation. There was also a similar question on the go-nuts mailing list some days ago and Nigel has posted an updated version of this file there. You might want to try it :)
The easiest solution seems to save the image to disk, and execute convert from Image Magic to transform it. You can use a ram disk if you want extra performance.