Skip to content

Commit

Permalink
测试
Browse files Browse the repository at this point in the history
  • Loading branch information
TC999 committed Nov 26, 2024
1 parent 39cced8 commit e60c558
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
24 changes: 14 additions & 10 deletions src/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dirs_next as dirs;
use std::fs::{self};
use std::sync::mpsc::Sender;
use std::sync::{mpsc::Sender, Arc, Mutex};
use std::thread;

pub fn scan_appdata(target: &str, selected_target: &str, tx: Sender<(String, u64)>) {
// 获取应用数据路径
let appdata_dir = match dirs::data_dir() {
Some(path) => match selected_target {
"Local" => path.parent().unwrap().join("Local"),
Expand All @@ -18,21 +17,23 @@ pub fn scan_appdata(target: &str, selected_target: &str, tx: Sender<(String, u64
println!("开始扫描文件夹: {}", target);
println!("扫描的路径: {}", appdata_dir.display());

// 确保路径存在
if appdata_dir.exists() {
// 创建一个新的线程来进行扫描
// 克隆 tx,使其可以在不同线程中共享
let tx = Arc::new(Mutex::new(tx));

// 创建新线程进行扫描
thread::spawn({
let tx = tx.clone(); // 克隆发送者
let appdata_dir = appdata_dir.clone();
let tx = Arc::clone(&tx); // 克隆 Arc 中的 tx
move || {
let entries = match fs::read_dir(appdata_dir) {
Ok(entries) => entries,
Err(_) => {
eprintln!("无法读取目录: {}", appdata_dir.display());
//eprintln!("无法读取目录: {}", appdata_dir.display());
return;
}
};

// 遍历目录中的文件和文件夹
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
Expand All @@ -42,8 +43,12 @@ pub fn scan_appdata(target: &str, selected_target: &str, tx: Sender<(String, u64
.unwrap_or("<未知文件夹>")
.to_owned();
let size = calculate_folder_size(&path);
if tx.send((folder_name, size)).is_err() {
eprintln!("发送数据失败");

// 尝试发送数据
let send_result = tx.lock().unwrap().send((folder_name, size));
match send_result {
Ok(_) => {},
Err(e) => eprintln!("发送数据失败: {}", e), // 输出错误信息
}
}
}
Expand All @@ -53,7 +58,6 @@ pub fn scan_appdata(target: &str, selected_target: &str, tx: Sender<(String, u64
}
}

// 计算文件夹的大小
fn calculate_folder_size(folder: &std::path::Path) -> u64 {
let mut size = 0;
if let Ok(entries) = fs::read_dir(folder) {
Expand Down
22 changes: 14 additions & 8 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use crate::confirmation;
use crate::delete;
use crate::scanner;
use crate::utils;
use eframe::egui::{self, Grid, ScrollArea};
use std::sync::{Arc, Mutex};
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::mpsc::Sender;
use dirs_next as dirs;
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use eframe::egui::{self, Grid, ScrollArea};
use dirs_next as dirs;

#[derive(Clone)] // 添加 Clone trait 以支持克隆
pub struct AppDataCleaner {
Expand Down Expand Up @@ -57,16 +59,20 @@ impl AppDataCleaner {

// 添加 start_scan 函数作为 AppDataCleaner 结构体的一部分
fn start_scan(&mut self) {
let (tx, rx) = mpsc::channel();
let (tx, rx) = mpsc::channel(); // 创建通道

// 将 `self` 包装在 `Arc<Mutex<Self>>` 中,允许线程共享
let appdata_cleaner: Arc<Mutex<AppDataCleaner>> = Arc::new(Mutex::new(self.clone()));
// 将 `tx` 和 `rx` 包装在 Arc<Mutex> 中,使其在多个线程中安全共享
let tx = Arc::new(Mutex::new(tx));
let rx = Arc::new(Mutex::new(rx));

std::thread::spawn({
let appdata_cleaner = Arc::clone(&appdata_cleaner); // 克隆 `Arc`,允许线程访问
// 创建线程进行扫描
thread::spawn({
let appdata_cleaner = Arc::clone(&self); // 克隆结构体,传入线程
let tx = Arc::clone(&tx); // 克隆发送者
let rx = Arc::clone(&rx); // 克隆接收者
move || {
let appdata_cleaner = appdata_cleaner.lock().unwrap(); // 获取锁
scanner::scan_appdata(&appdata_cleaner.selected_target, &appdata_cleaner.selected_target, tx);
// 可以在此处增加接收数据的逻辑,例如通过 rx.lock().unwrap().recv() 来接收数据
}
});
}
Expand Down

0 comments on commit e60c558

Please sign in to comment.