Jump to content

Need a nice way of closing uTorrent from my program


Ger Teunis

Recommended Posts

I've created a program that tries to close some programs on my computer.

Because uTorrent is minimized in tray; it's process doesn't have a MainWindowHandle to send windows (WM_*) message to.

At the moment I'm killing the process, but when restarting the application the .torrent files aren't correctly opened because uTorrent was killed instead of closed nicely.

Is there a standard way I am able to close uTorrent nicely without using the process.kill()?

With best regards,

Ger Teunis

Solution

I've created a program to close other processes, also closes uTorrent nicely instead of killing. It is a console application, download here

(please add a post here while downloading!)

Usage: CloseApp [/F] [/T=xx] filename

/F will force close (kill) of all other close methods do not work

/T=xx will override the default wait timeout on application close. Default in XML file (10secs)

Please note: filename must contain the complete path; when path contains spaces use quotes around it.

Example:

C:\>CloseApp "c:\program files\uTorrent\uTorrent.exe"

Link to comment
Share on other sites

Thank you for your reply.

Windows' SendMessage needs a window handle to send the message to.

Because uTorrent is minimized it doesn't have a windowHandle (0 or null).

At least the process info doesn't contain a valid window handle anywhere when minimized.

When uTorrent isn't minimized to tray I can close it correctly using it's Window Handle.

Link to comment
Share on other sites

Link to comment
Share on other sites

Thanks for your quick reply and help.

Sadly enough uTorrent doesn't have a message loop running when it is running minimized in tray.

I've enumerated all active windows currently running in windows, I've found one uTorrent windowHandle when uTorrent is minimized.

This windowHandle doesn't have a parent window. Sending a WM_CLOSE doesn't do anything, I guess no message loop in that window.

When I open the uTorrent main window (thus not minimized to tray), I get the main window's windownhandle, which has a messageloop. This messageloop does react to WM_CLOSE but because I enabled option "Close to tray" it will just close the main window and will run in "minimized in tray".

When I disable "close to tray" it closes correcly.

So, WM_CLOSE only works when the uTorrent's window is opened AND "close to tray" is disabled.

It would be great if the one window that is available when minimized had a messageloop which would really close uTorrent.

Link to comment
Share on other sites

Great, thanks. WM_ENDSESSION seems to work on application which are Minimized to tray.

I am trying to make a generic procress close routine, so I only have a filename.

This solution which seems to work:

- If process has an mainwindow> send WM_CLOSE message to mainwindow.

- If process not closed OR no mainwindow handle> send WM_ENDSESSION to every window found while enumerating every window which process is uTorrent's process.

- Even when this doesn't close, kill process.

Thanks jewelisheaven and alus.

Link to comment
Share on other sites

That is exactly why I started this post, before I just had to kill the process instead of nicely asking it to close (WM_CLOSE / WM_ENDSESSION). With the above solution it closes nicely all the time. (except when the application doesn't react to windows messages, it just gets killed as last resort).

Currently uTorrent correctly uses WM_CLOSE and WM_ENDSESSION. No more rechecks etc.

Link to comment
Share on other sites

  • 6 months later...

Hello, Ger Teunis

If that bit of wonderful coding you have just done to gracefully close down uTorrent works on WinXP would there be any chance of (cough, cough) getting a copy of the exe from you? Please. Mmm.... Pretty please even? (I'm not a programmer so wouldn't have a clue on where to start with it.)

Reason I'm asking is that I'm finding the uTorrent scheduler inadequate for setting up multiple rules in response to the way my ISP is constantly changing its speed and volume limits on upstream/downstream use at various times in the day.

Given that the scheduler as it stands can't deal with this I thought a way to get round it would be just to have uTorrent save out different 'settings.dat' files with the settings made in Scheduler appropriate for a given time period of the day. Once I got the variety of 'settings.dat' files that I needed I could then switch them in and out of the uTorrent folder using a batch file and some deft renaming. But to get uTorrent to auto-read the (now 'new') settings file I would have to do the following from a within a batch file. I would use an external scheduling program to launch that batch file:

1. shutdown uTorrent.

2. have the batch file switch the 'settings.dat' file according to the time of day period enforced by my ISP.

3. restart uTorrent - which will now read the (new) settings.dat file.

I've managed to set this up and it works fine except for the results of shutting down uTorrent from a batch file.

I've messed with 'taskkill' - got it to work but on restart of uTorrent it seems the shutdown wasn't graceful and so it checks all the torrent files again. Likewise, it was suggested elsewhere in the forum that 'Process.exe' using the -q switch might be worth a try. Tried it and got the same result as with 'taskkill' when uTorrent is restarted.

I've got it all working all I really need is something that will close down uTorrent gracefully so the restart isn't time consuming. Any chance?

P.S. I have a suspicion that a fair number of other people would find your new exe a treat to have around.

Link to comment
Share on other sites

If that bit of wonderful coding you have just done to gracefully close down uTorrent works on WinXP would there be any chance of (cough, cough) getting a copy of the exe from you? Please. Mmm.... Pretty please even? (I'm not a programmer so wouldn't have a clue on where to start with it.)

That piece of code was part of a battery saver (notebook) application I've created.

I can imagine that the close part may be useful for others so I've copy-pasted the code in a new console application.

While I was working on it I encountered a situation that uTorrent wasn't reacting while requesting it to close, that should be fixed.

The application was written in .net 2.0. So you need to have .NET framework 2.0 installed (like everybody should have by now!)

Download 1.0.1

Download

(Please leave a message while downloading)

There is a configuration XML file, which only contains one config option: amount of time to wait while closing the application using normal close. Default 60 secs.

It tries to close normal, if this doesn't work: by WM_CLOSE to all open windows of the application and as a last resort KILL the application (last is optional).

Usage: CloseApp [/F] application

I.e. CloseApp "d:\internet apps\utorrent\utorrent.exe"

(use quotes when using spaces in application path!)

the optional /F will, when all other 2 possibilities do not work, kill the application the hard way.

Link to comment
Share on other sites

Hello Ger,

Many thanks for the download. Sure helped my thinking juice. Here are my results in quickly testing it:

First Try (no /F - uTorrent in SysTray only):

==============================

Application does not have a open window: will search for windows to close.

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Process NOT closed, use force option to close.

Done...

RESULT = uTorrent not closed down.

Second Try (used /F - uTorrent in SysTray only):

=================================

Application does not have a open window: will search for windows to close.

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Closing window: uTorrent 1.7.7

Process NOT closed, closing application by force (kill).

Done...

RESULT = uTorrent closed down but on restart it (force) re-checks the torrent.

Third Try (used /F - with main window open on desktop):

=================================

Closing application and waiting for response...

Timeout while closing application; will search for windows to close.

Closing window: M

Closing window: Default IME

Closing window: uTorrent 1.7.7

Process NOT closed, closing application by force (kill).

Done...

RESULT = uTorrent closed down but on restart it (force) re-checks the torrent.

================================================

[Think! Think!]

Above done with setting in uTorrent to always 'Close to tray' enabled (my default setting).

Above done with setting in uTorrent to always 'Minimise to tray' enabled (my default setting).

Test with 'Close to tray' disabled - 'Minimise to tray' left enabled.

Also with uTorrent only showing in the SysTray (normal operation for me)

/F option NOT used:

====================================================

Application does not have a open window: will search for windows to close.

Closing window: M

Closing window: Default IME

Closing window: uTorrent 1.7.7

Application is closed.

Done...

RESULT = uTorrent closed down, on restart it does not (force) re-check the torrent.

(Did a manual force re-check - no probs.)

SUCCESS!!!!! :-)

===================================================

So with 'Close to tray' enabled and no /F option used looks like four attempts made to close uTorrent. Some kind of 'loop-y' thing, maybe? Keeps closing it to the SysTray - have another go, etc.?

In any case, Ger, it works. You go to the top of my admiration list for a month!

Have one question: What is 'window: M' and 'window: Default IME'?

P.S. After doing this I tried a 'taskkill' from DOS without its /F option. Guess what? It worked too - spent a whole night last night banging away at it with no success. So, 'Close to tray' issue there as well.

That said, your app is still useful and worth keeping.

Very many thanks, Ger. Very much appreciated. Problem solved. ISP's be damned! If you do any more on it I'd pretty please for a copy again. :-)

Link to comment
Share on other sites

Oh yeah, I forgot to mention: disable "Close to tray".

That option does exactly what it states: when it receives a close from windows (or the user for that matter) it will suppress the close and 'minimize' to the tray. This will prevent me from closing the application.

The /F is the hardest way to kill the application, not notifying or asking the application to close but doing it for him. This way the application does not have a clue he is being closed and is not able to do his last save actions (like the hash states).

My compliments for finding the "Close to tray" option!

Someday I'll re-implement the WM_ENDSESSION / WM_QUERYENDSESSION methods again, perhaps these will solve the problem you solved with the "close to tray" workaround.

About the "Default IME" and the other *UI, it seems to be a "window" (which has a different meaning in WIN32 software than what you might expect) created by windows itself I guess. Did not go into detail on the research on that part. I should when I where to official release the CloseApp. That's not the case, just a quick hack for all the interested ;-)

Link to comment
Share on other sites

Arg, I've encountered a small bug. Also supports "Close to tray" option in utorrent now.

Fixed in v1.0.1

Download

(Please leave a message while downloading)

Note:

Default wait time is 10 seconds, can be overridden by /T=60 switch or changing the config XML file.

/F = force close

/T=xx = Waits xx seconds for application close

Link to comment
Share on other sites

Hi Ger

Got it.

What I was finding was that I couldn't easily ditch my own old habit of clicking the 'Close (button)' to minimize uTorrent to the SysTray. So, since changing the setting, I've been inadvertently closing it down thinking I was minimizing. :rolleyes: Don't know why I do this as this is the only app I minimize in that way. I had even instigated a re-training programme for myself.

Thing I can see that might be useful is a switch to allow 'auto-exit' from the cmd window on a successful close of whatever app. And maybe even a switch to auto-exit even on a fail to close. With something in the xml to allow user to set default actions for these if no switch used.

Thanks again. Nicely done.

Link to comment
Share on other sites

Thing I can see that might be useful is a switch to allow 'auto-exit' from the cmd window on a successful close of whatever app. And maybe even a switch to auto-exit even on a fail to close. With something in the xml to allow user to set default actions for these if no switch used.

A bat file should automatically close the cmd window after running? Or didn't I understand you correctly?

Link to comment
Share on other sites

Sorry Ger

It is me that is incorrect. It closes just fine. I was just impatient on waiting on the time out. I don't work uTorrent very hard so close for me is more or less instantaneous.

Have toyed around with 'closeapp' and 'taskkill'. Yours is much more reliable than taskkill and a tad easier to use too.

Many thanks.

Link to comment
Share on other sites

No problem, hope it helps you and some others as well.

I was just impatient on waiting on the time out.

Sometimes it takes a while for uTorrent to close. It takes up to 15 seconds on my computer, but I've got a lot of torrents opened.

CloseApp will directly return and close when uTorrent is finished closing (or when the timeout is being triggered, but this should never happen!).

Link to comment
Share on other sites

  • 2 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...