1

I have made a launcher for my game Privateers. It works perfectly, downloads everything needed - But, for some reason, it freezes the entire computer!

The freeze usually occurs when doing something, if I afk at my computer until the download completes, then nothing happens. However, when I tested it on my mothers computer playing the game "World Of Tanks", the computer froze almost immediately. If I play a game then the launcher also has a tendency to freeze my computer.

I use windows 8, my mother uses windows 7.

On my own computer, when this happens I am able to move the mouse very slowly (between 30 second to 2 minute delay), alt+tab won't work, control+alt+delete will work (but when opening task manager the task manager does not appear). On my mothers' computer it is basically the same except that EVERYTHING is frozen 100% except for the mouse which is working fine.

It only happens when downloading large (5MB+) files. When my launcher downloads smaller files there is no issue.

I use the following code to download files:

void download(String source, String destination, int size) {

    File ofile = new File(System.getProperty("user.dir") + "", destination);
    System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
    try {
        if (ofile.exists()) ofile.delete();
        if (!ofile.createNewFile()) {
            throw new IOException("Can't create " + ofile.getAbsolutePath());
        }

        int inChar = 0;
        URL url = new URL(source);
        InputStream input = url.openStream();
        FileOutputStream fos = new FileOutputStream(ofile);
        for (int i = 0; i < size && inChar != -1; i++) {
            int percentage = (int) ((i * 100.0f) / size);

            progressBar.setValue(((int) ((percentage * 100.0f) / 100)));
            fr.setTitle(ofile.getName() + ": " + progressBar.getValue() + "%" + " Total: " + oprogressBar.getValue() + "%");
            inChar = input.read();
            fos.write(inChar);
        }

        input.close();
        fos.close();
        System.out.println("Downloaded " + ofile.getAbsolutePath());
    } catch (EOFException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I have been unable to find a duplicate of this happening when searching on the internet. Any help is appreciated.

Joehot200
  • 1,070
  • 15
  • 44

2 Answers2

3

Maybe multithreading will help you out over here.

See more about it in this post

Community
  • 1
  • 1
SteBra
  • 4,188
  • 6
  • 37
  • 68
  • That was fast - An upvote to you for the good idea. I will accept the answer if it solves the issue, though I personally suspect it will. – Joehot200 Nov 03 '14 at 15:45
  • 1
    It works, but it looks like a downloader on meth :P. http://gyazo.com/591881f0a19e5d79ddaf60d603f2a936 (Turn it to MP4 mode) It isn't perfect, and I am having issues displaying the download % - However, it works :). – Joehot200 Nov 03 '14 at 16:28
1

Buffer the input stream, or read more than one character at a time, or both.

bmargulies
  • 97,814
  • 39
  • 186
  • 310