|
I am doing some asynchronous operation(Webclient.DownloadFileAsync() ) which is working on a separate thread. This will raise some events when the download process finished. Is it possible to stop the current thread until these events are fired ? Inside the event handler, I need to resume the thread. Any idea's ?
|
|
|
|
|
Hello,
If you mean Stop and Start the MainThread and you have the instance of the main Window for example.
You could call something like this from your working thread.
private void StopThread()
{
if(InvokeRequired)
{
Invoke.....
return;
}
Thread actThread = Thread.CurrentThread;
actThread.Suspend();
}
All the best,
Martin
|
|
|
|
|
Hi,
I am doing unit test with NUnit. The application which I am testing is a downloader which downloads files from remote server. So once the downloading finishes, I need to assert the file size. But since the download process is happening in the other thread, NUnit GUI is not waiting until download completes. Hence I was not able to test the events. I managed it by running the download class in another thread, just below to that I used Thread.Sleep() to make NUnit GUI wait for some time. By this time, the thread which I created will finish the download work and calls all events. So I can test the events too.
Anyway thanks for your suggestion.
|
|
|
|
|
i'd catch the donwloadcomplete event in unit-test and setting a flag (complete=true).
you can start the test then as follows:
<br />
[Test]<br />
public void TestDownload()<br />
{<br />
Downloader.DownloadReady += ...
Downloader.StartDownload(path)<br />
<br />
while (!complete)<br />
Application.DoEvents();<br />
}<br />
|
|
|
|
|
WOW ! That's great. Thanks for pointing this out
|
|
|
|
|
Calling Application.DoEvents is bad practice. It often results in unexpected results, so I would really advice against it.
If you can get a handle to the thread I'd use its Join method. If this isn't the case, you can use an AutoResetEvent to signal from one thread to the other. You can read more about it here[^].
Standards are great! Everybody should have one!
|
|
|
|
|
Another simple way is to use Thread.Join() method.....
Muhammad Talha
|
|
|
|
|
The way that I have implemented many solutions for this is to use a ManualResetEvent to pause the thread. Then when the work is complete in the second thread, set the ManualResetEvent allowing the first thread to continue.
Phil
|
|
|
|
|
Hi,
I'm sitting with a weird problem.
I created an application in C# 2005 Express. It has 3 timer controls in it.
The first timer runs as a clock (system clock) and once this timer reaches a specific time during the day it runs its procedure (this works fine)
The 2nd timer has its Interval set to 10min. So every 10 minutes it needs to run its procedure.(doesn't do it)
The 3rd timer has its Interval set to 10Sec. So every 10 second it needs to run its two procedures (doesn't do it either);
Now when i run this app in debug mode everything works perfect. Breakpoints are being reached all the time, I don't see any problems. But once i publish the app and install it on a users machine it doesn't always run the procedures
As u probably realized is that this application is an auto update app which auto updates data (retrieve data from webservice then update the local SQL database)
Any help would be greatly appreciated. I need to get this working by midday tomorrow.
Any other suggestions would be great.
Thanks
|
|
|
|
|
Why don't u use threads to run your code. You can use your time to control the different threads in your app.
|
|
|
|
|
I was doing this but gets too complicated to run one procedure say at 13:00 everyday and 2 other procedures on different time intervals.
If i run a procedure under a timers tick event, isn't that set in its own thread too?
I'm just worried that if two different procedures run at the same time so they don't crash or something.
|
|
|
|
|
try logging some "I'm in this method" statements and see which code is not executed.
debug mode may work since one breakpoint pauses all threads.
|
|
|
|
|
I did that by using message boxes and it worked but then sometimes it just doesn't work.
I have changed some values it seems to be working now but i still feel unsafe.
Will do more tests thanks.
|
|
|
|
|
Hi,
When I'm trying to execute the following code, it raises exception saying that "Cross-thread operation not valid: Control 'button1' accessed from a thread other than the thread it was created on."
private void Form1_Load(object sender, EventArgs e)
{
Thread th = new Thread(threadStartServer);
th.Start();
}
private void threadStartServer()
{
string result = "";
TcpListener tcpListener1 = new TcpListener(8080);
tcpListener1.Start();
button1.Text = "Success"; // Here the exception(mentioned in subject) occurs.
}
Please let me know the solution. Thanks in advance.
|
|
|
|
|
Hello,
As the error message says it all.
Here is a solution using "InvokeRequired" and "Invoke" (google it)
private void threadStartServer()
{
string result = "";
TcpListener tcpListener1 = new TcpListener(8080);
tcpListener1.Start();
EditButtonText("Success");
}
private delegate void EditButtonTextDelegate(string strArg);
private void EditButtonText(string actText)
{
if(InvokeRequired)
{
Invoke(new EditButtonTextDelegate(EditButtonText), new object[] {actText});
return;
}
button1.Text = actText;
}
Hope it helps!
All the best,
Martin
|
|
|
|
|
Thanks for the reply... and helped me get more familiar with the delagate/invoke technique...
|
|
|
|
|
You can also use BackGroundWorker class which has ProgressChanged event, allows to access your GUI items.
|
|
|
|
|
Thanks for the immediate reply... and helped me get more familiar with the delagate/invoke technique...
|
|
|
|
|
LalithaSJ wrote: button1.Text = "Success"; // Here the exception(mentioned in subject) occurs.
You can't access the controls which is in the main thread from a thread which is created by you. Check this[^]
|
|
|
|
|
Hello Friends,
How can i create 1D image in C#.NET.
|
|
|
|
|
Dikshant wrote: How can i create 1D image in C#.NET.
Have a think about what 1D means. How would you want to represent that?
A string is one dimensional. An image is not.
|
|
|
|
|
Colin Angus Mackay wrote: Have a think about what 1D means. How would you want to represent that?
A string is one dimensional. An image is not.
What would an image of a string be?
Knowledge is hereditary, it will find its way up or down. - Luc Pattyn
so you answer don't be scared of failure
The only failure is never to try
Things You've Never Done - Passenger -2008
|
|
|
|
|
Two dimensional. Like your browser window...
Heres a one-dimensional image library:
class OneDeeImage
{
private Color[] data;
public OneDeeImage(int width)
{
data = new Color[width];
}
}
|
|
|
|
|
i am facing a problem when m trying to send mail through remoting as system.net.mail.mailmessage is not serializabe..plz help me out
thanx
|
|
|
|
|
i think u should implement the system.net.mail.mailmessage at remoting server and from client side just send the simple text to server ....
than server sends the mail for u
|
|
|
|