Powershell: Freezing GUI - multithreading

Just a quick one. I have a problem with a simple tool that I've created that gets the CPU usage for a set amount of time with a small box that appears to display the % of CPU being used (I've stripped out the GUI for the code below).
function loop
{
$get = read-host
for($start = 0; $start -le 100;$start++)
{
cls
$pro_percentage = Get-WmiObject win32_processor -computer $get -property Loadpercentage | select loadpercentage
$percentage = "Processor usage is: " + $pro_percentage.loadpercentage + "%"
$percentage
}
}
loop
There's a few other bits to the GUI that I've created but I've noticed one majour problem, and this goes for other GUI apps that I have created. Whenever the 'go' button is pressed to start the script from the dialog box, the GUI freezes. The menu bar that I've created inside the GUI freezes and no other button can be pressed.
Am I supposed to run each function in a different thread? Something like that?
Thanks

Try to add:
[System.Windows.Forms.Application]::DoEvents()
in your loop. Quick and dirty, however.

Related

Need help in GMS2 pls, trigger works again and save menu opens again, even though it should just close

I've run into this problem.
I have a save menu with two options: "Save" and "Return", which is selected by the option heart. When selecting the "Return" option, the option arrow going back to "Save" even though it should close the save menu(It seems that when menu is closing, the trigger works again and menu opens again, even though it should just close).
The second problem is that when you select the "Save" option, the "File saved" window appears, after which it does not disappear when im pressing "Z" key, and I do not understand what is the reason for these two errors, no matter how I tried to fix them.
It seems that I write the code correctly, but nevertheless it does not work.
Key Press "Z" Event:
if image_index = 0
{
instance_change(obj_saveconfirm, true);
audio_play_sound(snd_save, 10, false);
}
else {
if image_index = 1
{
instance_destroy();
}
}
Code that opens the save menu:
if place_meeting(x, y, obj_player) && keyboard_check_pressed(ord("Z")) && !instance_exists(obj_savemenu) && !instance_exists(obj_saveconfirm)
{
instance_create_depth(x, y, -9999, obj\_savemenu);
}
It seems that I write the code correctly, but nevertheless it does not work.
I also used "keyboard_check_released" in code that opens the save menu, and menu is closing, but after half a second it opens again.

A problem for selecting a image from the imagepupop dialog

One more question. If I create a image-popup dialog, I find it only works when the frontimage (the top one in the image list). If other image is selected, the program will report "the image used in the expression does not exist". I can not understand the logic behind this error.
The following is a modified code pasted in the answer of the previous question. It can work well if the first image is selected, but the error message appears if the second image is selected.
I use GSM 2.30.xxxx
Class CMyDLG : UIframe
{
TagGroup DLG,DLGItems,imgPop
object Init(object self)
{
DLG = DLGCreateDialog("Test",DLGItems)
imgPop = DLGCreateImagePopup()
DLGItems.DLGAddElement( imgPop )
return self.super.init(DLG)
}
image GetSelectedImage( object self )
{
string selectedImageLabel
imgPop.DLGGetValue(selectedImageLabel) //DLGGetValue can return the label of the image diretly
Result("\n" + selectedImageLabel)
// From the string, get the label
//string label = selectedImageLabel.left( selectedImageLabel.find(":") )
//Result("\n" + label)
// From label, return image
//return FindImageByLabel(label)
return FindImageByLabel(selectedImageLabel)
}
}
// main
{
object dlg = Alloc(CMyDLG).Init()
dlg.Pose()
image selected = dlg.GetSelectedImage()
if ( selected.ImageIsValid() )
{
selected.SetName( "Selected" + random())
selected.ShowImage()
}
else Throw( "Error, nothing selected." )
}
Using the test code on GMS 3.3 it works except for the bug mentioned. I presume it's the same for GMS 2.3 but I haven't verified.
To make sure we test the same thing, here are exact instructions and a break-down:
Start out with two images A and B and A being front-most.
Run script
Don't change anything in the dialog
Press OK
ERROR
The dialog - taggroup does not (yet) hold any value. It possibly should, I consider this a bug.
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "A" from the drop-down
Press OK
A is correctly selected
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "B" from the drop-down
Press OK
ERROR
The dialog - taggroup does not (yet) hold any value. It definitly should, I consider this a bug. It is most likely what you were describing?
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "A" from the drop-down
Click the selection box and select "B" from the drop-down
Press OK
B is correctly selected
To summarize:
Yes, there is a bug and nothing wrong with your script.
The selection box only works after selecting an items for the second time.
The example code (first script) in this answer seems to work on any of the open images when selected.
However, there is the (mentioned) bug that it does not work on first selection, only when you select one image and then another.
If your code fails, please provided a slimmed-down code-example of the failing code so that a mistake can possibly be spotted.

System buttons changing to Windows 2000 style

I have an old VC6 application to maintain.
When it starts, it shows the system buttons (minimize, maximize, close) in the usual Windows 7 or Windows 8 style.
As soon as I do an operation that triggers a redraw (SetMenu, move the window…), these buttons change to a Windows 2000 style (just gray boxes with black icons). We have other applications (VC6 as well) that do not have this behaviour.
Any idea where I should start to look ?
Edit:
After more investigation, I found that a call to DrawFrameControl is causing all this trouble:
CWindowDC dcWnd(this);
CRect rectWindow, rectClient;
GetWindowRect(&rectWindow);
m_rectDisconnectButton.left = rectWindow.Width() - DISC_BUTTON_WIDTH 6;
m_rectDisconnectButton.top = ::GetSystemMetrics(SM_CYCAPTION) + 4;
m_rectDisconnectButton.right = m_rectDisconnectButton.left + DISC_BUTTON_WIDTH;
m_rectDisconnectButton.bottom = m_rectDisconnectButton.top + BUTTON_HEIGHT;
if ( m_bDisconnectButtonPressed )
dcWnd.DrawFrameControl(&m_rectDisconnectButton, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
If I comment the last call, obviously my button will not be drawn, but I no longer have the issue with the "system buttons".

Keep a progress bar on top. Linux

Okay so I was just wondering if there is a way to have a progress bar on the top as the script is running to tell the user that x% of the process has been completed. Also is there a command that is built into the OS that would let me use a progress bar or would i have to design that as well in code?
Include in your script a percentage tracker, but will consume more resources...
For example if you are copying a bunch of files:
`
foreach $y (#files) {
$countline++;
$total = 1000;
$lastcount = $currentcount
$currentcount = $countline / $total
if ( $lastcount > $currentcount ) {
$progress_display == 1
$currentcount
if ($progress_display == 1) {
print ".";
}
}
print "\n";
`
There is zenity, a GTK application with GUI elements for scripts. It has a progress bar and much more.
The package dialog provides several UI elements for shell scripting. One of them is a gauge, a progress bar. Debian maintains a simplified version of dialog called whiptail.
There is also an X version of dialog: Xdialog, but I have never seen it myself.

How to exit program with close button in XCB

Can not find any reference on how to close application by the "X" button. I'm programming using XCB and want to close the program by the "X" button. I looked and can't find anything about it. I know how to close by pressing a button. Also, by pressing the "X" button the window looks like it closes but doesn't.
I struggled on this topic some time ago as well.
Look at http://marc.info/?l=freedesktop-xcb&m=129381953404497 .
The key is to store the cookie for the WM_DELETE_WINDOW in a separate cookie...
xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(c, 0, 16, "WM_DELETE_WINDOW");
xcb_intern_atom_reply_t* reply2 = xcb_intern_atom_reply(c, cookie2, 0);
and in the event loop compare the client_message with the cookie2
case XCB_CLIENT_MESSAGE:
{
if((*(xcb_client_message_event_t*)event).data.data32[0] == (*reply2).atom) ...
}

Resources