العملية الرئيسية مقابل عملية العرض
استكشف أدوار ومسؤوليات العمليتين الرئيسية وعمية العرض، وكيفية تفاعلهما لتشكيل تطبيق Electron متكامل
العملية الرئيسية مقابل عملية العرض درس مجاني في Electron Desktop App Development على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Electron Desktop App Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Electron Desktop App Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Electron's Dual Nature
Welcome! Electron lets you build desktop apps using web technologies. But it's not a simple web browser. It uses a unique dual-process architecture to make this possible.
You'll learn about the two main types of processes: the Main Process and the Renderer Process. Understanding their roles is key to building robust Electron applications.
Main Process: The Conductor
Think of the Main Process as the conductor of your Electron application. It's a Node.js environment, meaning it has full access to Node.js APIs and can interact directly with the operating system.
There's usually only one Main Process running per Electron application.
Main Process Responsibilities
The Main process is responsible for core application tasks, including:
- Creating and managing browser windows: Each window in your app is controlled by the Main process.
- Handling application lifecycle events: Such as when the app starts, quits, or becomes active.
- Accessing native OS features: Like creating menus, opening dialogs, and interacting with the file system.
- Managing application-wide resources: Settings, global state, etc.
Main Process in Action
This is a typical main.js file, the entry point for an Electron app. It creates and manages windows. Notice it uses Electron's modules and Node.js features.
const { app, BrowserWindow } = require('electron');
const path = require('path');
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false, // Keep false for security
contextIsolation: true // Keep true for security
}
});
// Load the index.html of the app.
mainWindow.loadFile('index.html');
// Open the DevTools.
// mainWindow.webContents.openDevTools();
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.whenReady().then(createWindow);
// Quit when all windows are closed, except on macOS.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});Renderer Process: The UI View
The Renderer Process is where your user interface lives. Each Electron window (created by the Main Process) runs its own dedicated Renderer Process. It's essentially a complete Chromium browser instance, just like a tab in Chrome.
This is where your HTML, CSS, and JavaScript for the UI are executed.
Renderer Process Responsibilities
The Renderer process focuses on displaying content and interacting with the user:
- Rendering web content: It takes your HTML, CSS, and JavaScript and displays it visually.
- Handling user interactions: Responding to clicks, keyboard input, form submissions, etc.
- Executing web APIs: All standard browser APIs like DOM manipulation, Fetch API, Web Workers, etc., are available here.
- Limited direct OS access: For security, it does not have direct access to Node.js modules or native OS features by default.
Renderer Process in Action
This index.html is loaded by the Main process into a window. The JavaScript inside it runs within the Renderer process, just like in a web browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Electron App UI</title>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; text-align: center; padding: 50px; background-color: #f0f2f5; color: #333; }
h1 { color: #2c3e50; }
p { margin-top: 20px; font-size: 1.1em; }
code { background-color: #e0e0e0; padding: 2px 4px; border-radius: 3px; }
</style>
</head>
<body>
<h1>Hello from the Renderer!</h1>
<p>This content is displayed by Chromium inside an Electron window.</p>
<p>The script below runs in the <code>Renderer Process</code>.</p>
<script>
document.addEventListener('DOMContentLoaded', () => {
const messageDiv = document.createElement('div');
messageDiv.innerHTML = '<p><b>DOM Content Loaded!</b> This is a Renderer process event.</p>';
document.body.appendChild(messageDiv);
console.log('Renderer process is ready!');
});
</script>
</body>
</html>Main vs. Renderer: Key Differences
Here's a quick summary of the core distinctions:
- Environment:
Main: Node.js runtime
Renderer: Chromium web runtime - Instances:
Main: Single instance per app
Renderer: One perBrowserWindow - OS Access:
Main: Full Node.js & native OS access
Renderer: Limited, web-only APIs (by default) - Purpose:
Main: App lifecycle, window management, native features
Renderer: UI rendering, user interaction
Why the Dual Process Model?
This architecture isn't just for complexity; it offers significant benefits:
- Security: Isolates potentially untrusted web content from direct OS access. If a renderer process is compromised, the main process and system are protected.
- Stability: If a specific web page (renderer) crashes due to a bug, it won't bring down the entire application or other windows.
- Performance: Heavy tasks can run in the Main process (or separate worker processes), keeping the UI (Renderer) responsive and smooth.
Process Roles Check
Test your understanding of the Main and Renderer processes.
Recap: Main & Renderer
Great job! You've now grasped the fundamental architecture of Electron applications.
- The Main process (Node.js) is the control center, managing windows and accessing native OS features.
- Each Renderer process (Chromium) displays your web-based UI content within a window.
This separation provides security, stability, and performance. In the next lesson, you'll discover how these processes communicate with each other!
الأسئلة الشائعة
هل درس «العملية الرئيسية مقابل عملية العرض» مجاني؟
نعم — نص درس «العملية الرئيسية مقابل عملية العرض» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Electron Desktop App Development، انتقل إلى CoddyKit PRO. تتضمن دورة Electron Desktop App Development 4 دروس في المجموع.
ماذا ستتعلم في «العملية الرئيسية مقابل عملية العرض»؟
استكشف أدوار ومسؤوليات العمليتين الرئيسية وعمية العرض، وكيفية تفاعلهما لتشكيل تطبيق Electron متكامل تتمرن على Electron Desktop App Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Electron Desktop App Development؟
لا تُشترط خبرة سابقة. Electron Desktop App Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «العملية الرئيسية مقابل عملية العرض»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Electron Desktop App Development هذا؟
نعم. كل درس في Electron Desktop App Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- العملية الرئيسية مقابل عملية العرض
- الاتصال بين العمليات (IPC)
- حزم تطبيق Electron
- إدارة نوافذ متعددة